|
Hello and welcome to a "short" Flash tutorial! This tutorial explains two often used formulas to make smooth motion movements without the use of tweens - just math.In the examples the target position is the current mousecursor, but can be whatever you want. Note that Flash MX and FlashPlayer 6 (or higher) are required! 1 MoveTo Ok, the first one moves a symbol to a given position and decelerates the closer it gets to the target, similar to an easing out tween. Example:
speed = 0.2;
this.onEnterFrame = function() {
xtarget = _xmouse;
ytarget = _ymouse;
mycircle._x += (xtarget - mycircle._x) * speed;
mycircle._y += (ytarget - mycircle._y) * speed;
}
This is the complete code which goes in the first and only frame of the movie. mycircle is the instance name of the symbol you want to move. At the beginning we specify the variable speed which can be changed to anything between 0 and 1. Reasonable values are from 0.1 to 0.5 depending also on your framerate. The next block is inside a onEnterFrame function which will be executed once per frame. xtarget and ytarget specify the position where the symbol should move to and can be changed to whatever you like. The next lines are what is most important. This formula calculates a value which is the difference between the current position and the target. Here is a basic explanation:
2 Springle This one also moves the symbol to a new position but overshoots the target, springing back and forth until coming to a stop at its final destination. Example:
overshoot = 0.9;
speed = 0.1;
this.onEnterFrame = function() {
xtarget = _xmouse;
ytarget = _ymouse;
distx = (xtarget - mycircle._x) * speed;
disty = (ytarget - mycircle._y) * speed;
newx *= overshoot;
newy *= overshoot;
newx += distx;
newy += disty;
mycircle._x += newx;
mycircle._y += newy;
}
Since its very similar to the above i'll only cover the differences: overshoot specifies how far the symbol should over shoot the target position. newx and newy are the key-variables:
The main trick compared to the first example is to add one variable between which calculates the value that the symbol should head over. Note that this is an implementation of Hooke's Law. ;-) Have a lot of fun! <Commentbox>
Post a commment:
© 2003 -2005 Matthias Grumet | embege.com |
||||||||||||||||