actionscript physics stopper
- Started
- Last post
- 12 Responses
- d1ė
Im sure you are all familiar with the "inertia" actionsript that accelerates or slows down a moverment or fade in a natural manner (see old mowtown slider).
Well, my questions is this...
The script always runs in the background, even if the said MC has moved to its destination and appears to have stopped (it is really being repositioned over and over again, albeit in place). This taxes the CPU and servers no purpose.
Now, when trying to concoct scripts that stop the movement when it has reached it's destination, things get messey. i have not found a way to stop the script in it's intentded spot once the object has finished its movement. (yes I have tried the obvious).
If you know of, or have found a way to accomplish the above mentioned, I would LOVE to hear it.
thanks!
- ********0
I wish I knew that much actually
- unknown0
Well it's like this:
Since you already know that inertia divides the difference between the target distance and the object's current position the formula actually keeps dividing the next value over and over. That's how you get that slowing down effect. Well if you understand this then it becomes obvious that since we are dividing, it will be impossible to get to absolute zero. It will always have the decimals.
Now enough of theory, let's take a look at your case. So let's say you have an object that needs to stop at X:380. The formula does its thing and gets to almost near 380 (but never exactly). In this case you need to use math.round() or math.abs() in order to round the decimal number in order to use IF loop. I just can't remember which one exactly since I don't have a reference here. There is a math function that round to the nearest higher number. That's how you test to see if your object reached the targeted point. I've done this many times, and I can try to find a script in one of my projects if you can't solve it yourself.
Cheers.
- abizzyman0
http://www.friendsofed.com/books…
The very first chapter of this book solves your motion problem. You can force the script to stop running by writing a simple math function that forces the variable to reach it's actual destination.
This book rules, by-the-way. The very first chapte is on scripted motion!!!... imagine what comes next!
- d1ė0
thanks for the tip abizzyman
care to share that tip that they mentioned in the book?
Im up to par on everything else, no need for the friends of ed but thanks for the tip anyways!
- neeko0
math.ceil rounds up. to use math.floor or ceil, you'd always have to know whether your numbers were getting greater or smaller... theres a far more utilitarian way to do it.
- noneck0
onClipEvent(enterFrame) doesn't tax the server at all, unless you're doing a call to a script on it.
The taxation of a non-moving inertia script on a CPU is so small, it isn't even worth mentioning.
But that ain't really yer question, is it?
Most of the inertia scripts take the form of:
onClipEvent(enterFrame){
_x = (target._x - _x)/5;
}Which, as Boz kindly pointed out, won't ever really reach zero, but get very close. (Technically though, because flash automatically rounds to one decimal place, you would, eventually, reach zero. But I'm just being an ass now).
As my man neeko pointed out(sup, man?) the Math obejcts are only useful if you KNOW that you want to round up or down, so aren't as portable.
I would check to see if the difference between target._x and this._x are less than, say 1. If they are then this._x = target._x.
ie: it snaps into place if it's less than half a pixel out.
Let's put it together:
onClipEvent (enterFrame) {
//get pixels to move
movex = (target._x - _x)/5;//check if moving
//more than 1 pixel
if (1 < movex) {
_x = movex;
} else {
//moving less than 1 pixel
//snap in place
_x = target._x;
}
}But that's just how I'd do it. (Watch Colin Moock come along and show me stupid).
abizzyman is right, the friends of ed flash books are top notch. if you have the means, I'd highly suggest one. Though, I'd probably just go to the store and read them there, they suck as references.
- enobrev0
i agree with boz and noneck with every point except for friends of ed books. waste of time and money. they put less time into writing them than you will reading them, which shows if you try their faulty examples...
Besides that, put some more attention into math rounding. There's no need to deal with percentages and decimals in a half a frame of animation. (as Boz and No pointed out)
- jwll0
It's best to use math.abs so that it won't matter if the target is more or less than the current point. So something like this onenterframe:
var dif= this.targetx - this._x
this._x += dif / 5
if(math.abs(dif)<0.6){
this._x = this.targetX
delete this.onenterframe
}
- londonBoy0
A Simple prototype:
move_x = function(obj, newx, speed) {
obj.onEnterFrame = function() {
this._x +=(newx-this._x)/speed;
if(Math.ceil(this._x)==newx) {
delete this.onEnterFrame;
}
}
}This prototype goes on the actions for a frame. It is activated with:
move_x(myMovieClip, 320, 6);
which can be used on a frame action, or in conjunction with a buttons on(release) function.
what you are concerned with is the "if" statement and the delete function.
Check you email, I've sent you a .fla
- noneck0
That's a nice proto londonboy.
Even nicer portfolio. I love that tree.
- ********0
very useful advice for me here.
thank u all!
:)
- vizual0
Couldn't you just set a bounds on x?
Maybe I'm way off kilter here. You could though define a bounds for where x could exist. If it exceeds those bounds, stop?
or vice versa, define a bounds where x cannot exist, and if it enters theres bounds, stop.
the latter would be least efficienty tho imo.
heh just my 2 cents. ignore.