- Home
- Tutorials
- Flash
- Intermediate
- Actionscript Powered Movement
Actionscript Powered Movement

Page 2 of 5
- Where are we moving to? We need two numbers: our target _x and _y. These I generate using the Math.random() method.
- Where are we coming from? We need to know where our target is in relation to our current position. This we determine using getProperty.
- What's the distance between our target and our current position? This we determine with a simple mathematical equation, since our values are numbers (in terms of _x and _y).
- How many steps do we want to take to get there? This is an arbitrary value which will control how fast / smooth your animation is. If you wanted to do it in two steps, it will obviously just jump from one place to another. In my examples I've used 20 steps as it makes for a nice smooth, yet relatively fast tween.
- Now we know where we're going to and coming from, how far that is, and how many 'steps' it's going to take. The next thing we need to know is... that's right, how far we should move per step. Again this is just simple maths. If I want to move 20 meters in 20 steps, each step must be about 1 meter long right? Same principle applies here.
- "Are we there yet?". Yes, even computers ask annoying questions. To determine if we're there yet I use a counter variable which counts how many steps we've taken. If we're doing 20 and we've taken 18, we're not there! (Are we in India yet? :o)
Sounds simple huh? I think most of you could go off an make your own working example now, but the next problem people face is the syntax because it does get a bit ugly the way some people do it. I've kept it as clean as possible.
Now that we know the logic, let's go back to our example. Grab the source for the movie above. As always, if you can't open my source you're going to have to build it yourself. Those of you with the source can skip this bit. Those without please construct the following:
- A MC on the main stage with an instance name of 'circle'.
- A three frame MC (on the main stage) with an instance name of 'controller'.
loops = 0;
_root.target_x = Math.random()*450;
_root.target_y = Math.random()*300;
_root.xdiv = (_root.target_x-_root.circle._x)/20;
_root.ydiv = (_root.target_y-_root.circle._y)/20;
Frame 2 has the following actions:
loops++;
_root.circle._x += _root.xdiv;
_root.circle._y += _root.ydiv;
Frame 3 has the following actions:
loops++;
_root.circle._x += _root.xdiv;
_root.circle._y += _root.ydiv;
if (loops<20) {
gotoAndPlay(2);
} else {
gotoAndPlay(1);
}
That is all.
Spread The Word
Article Series
This article is part 1 of a 3 part series. Other articles in this series are shown below:
-
Actionscript Powered Movement
