How can make a moving object in flash constrain within the window/stage?
Do you mean keep its size without stretching or shrinking?
If so…
Stage.scaleMode = "noscale"
what i meant was…
for example…i have a racing car in the window…and i can move it using key press…but i do not wish it to go out of the window. So it means that the car can only move within the window…
How can dat be done?
Check its _x and _y properties to see if they exceed 0 or your window height or width.
erm…check the MC _x and _y properties? how can it be done?
Btw, how can I get an animation to play when the car is on top of a button or movieclip?
i’m still new to this…=|
we would need to know what method you’re using to move the car before that could be answered. Can you provide an FLA for us to look at?
ok…car is just an example…
Anyway, the object in this .fla is just a dummy…
In this case…I do not wish my moving object to go out of the window…and when its on top of the grey button…a pop up window will appear…
How can this be done with AS?
sorry… I got to check it in the morning. No Flash here at work.
its ok…=)
meanwhile…any other help?
well, if the car were moved by say buttons, where you held down the right arrow key and it moved to the right… then you would have code something like
onKeyPress(RIGHTARROW){
if(_root.car._x>0||_root.car._x<600){
_root.car._x+=10;
}
}
Keep in mind… this is just from my head, so it probebly wont work the exact way I’m thinking… but it shows in what way “if” statements can be used to prevent an object from going somewhere.
The idea of a movie clip playing while the car is over it would be done with a hitTest method. Again though… it will take me looking at it before I can tell exactly what the paths are to various objects in the movie, but it might look something like.
onClipEvent(enterFrame){
for(obj in _root){
if(_root[obj].hitTest(this,true){
_root[obj].play();
}
}
}
it probebly looks like greek to you at this point but I’ll explain both methods once I see how your movie is set up.
I tried the hit test method…didn’t work…=|
I’m so messed up…
yeah… it is very dependent upon the paths to various objects. The other thing is, the way I wrote it, it would basicaly be testing 17+ times a second, on every object on the main timeline. Though it might have worked it’s hardly efficient. Really, it’s just an example of what a hit test looks like, in a “for in” loop. I can actually explain relatively well what happens in the script I wrote.
//this would go on the car movieClip. onEnterframe fires it's code whenever the object's play head enters a new frame.
onClipEvent(enterFrame){
//a for loop says, look in here, for objects, and temporarily call each object you find, "obj". Perform the contained code on each object you find.
for(obj in _root){
//if the obj you found in root is intersecting with this object's registration point, exicute the script in the brackets.
if(_root[obj].hitTest(this,true){
//test was true, make this object you found play
_root[obj].play();
}
}
}
It’s not too tough to perform these hitTests, but like I said… not very efficient the way I wrote it.
heres some constrain code:
http://proto.layer51.com/d.aspx?f=660
just put that in the first frame of your movie so its defined and understood by flash, then, use whatever it is to move your car and AFTER that, use that function ie
car.constrainTo(screenClip);
^ now you can either constrain it to a movieclip (like a movieclip that makes up the ground) or hardcode in the dimensions of your screen like
car.constrainTo({xmin:0,xmax:550,ymin:0,ymax:400});
I copied this to the 1st frame…
MovieClip.prototype.constrainTo = function(cb){
if (typeof cb == “movieclip”) cb = cb.getBounds(this._parent);
var tb = this.getBounds();
if (this._x + tb.xmin < cb.xmin) this._x = cb.xmin + tb.xmax;
if (this._x + tb.xmax > cb.xmax) this._x = cb.xmax + tb.xmin;
if (this._y + tb.ymin < cb.ymin) this._y = cb.ymin + tb.ymax;
if (this._y + tb.ymax > cb.ymax) this._y = cb.ymax + tb.ymin;
}
and I put in the function
car.constrainTo
({xmin:0,xmax:550,ymin:0,ymax:400});
into my moving object…
but it still go out of the window…why?
ah…it works…Thanks!