|
|
Kabomb > Tutorials > Basic Movieclip
Control
Introduction
Shows how to control an MC to go left, right up, down and
diagonally. |
|
|
The Code
Firstly, create an MC on the stage of anything you want,
I started with a circle. Give it an instance name of 'ball'
On the main timeline, add this code:
|
Explanation
Firstly you set a variable 'speed' to any number, which
is going to be 5
The onEnterFrame function runs through a code every frame,
and anything outside of it runs only once at the very start.
Inside this, you check if the left, right, up, or down
arrow keys are down. So if the left key is down, the balls
'x' property (horizontal position) will have speed subtracted
from it (that is what the operator -= does). In other words,
the ball will move left 5 (or speed) pixels.
If the right key is down, it will move right 5 pixels,
If up is down, the ball's y position will go up 5 pixels,
and if down is down, it will go down 5 pixels.
The next lines check if the ball is within the Stages
boundaries. So if the ball's x is greater than the Stages
width (the very right of screen), it will return back to
0 (the very left of the screen) and vice versa.
The same also happens with the ball's y position using
0 and Stage.height as boundaries. |
Conclusion
Test the movie and use the arrow keys to control. The reason
the ball can move diagonally is because, for example, if
the right and up arrow keys are down, the ball's x goes right
and the ball's y goes up both at the same time, making a
diagonal movement. |