On your stage, draw a shape such as a ball.
Use the selection tool to select your shape.
Press F8 to convert it to a symbol. Make sure you convert it into a movie clip.
Give your shape an instance name such as myBall.
Right click on Layer 1 and insert a new layer.
Double click on Layer 2 and rename it to CodeLayer.
In frame 1 of the CodeLayer, add this code.
// ==== Listen for the enter frame event and run the myBallControl code each time. ====
addEventListener(Event.ENTER_FRAME, myBallControl);
// ==== GLOBAL VARIABLES FOR UP, DOWN, LEFT, RIGHT ====
var ud:int = 1; // 0 = down. 1 = up
var lr:int = 1; // 0 = left. 1 = right
function myBallControl(event:Event)
{
// ==== HAS BALL HIT RIGHT EDGE? ====
if (myBall.x > stage.stageWidth - myBall.width)
{
lr = 0;
}
// ==== HAS BALL HIT LEFT EDGE? ====
if (myBall.x < 0)
{
lr = 1;
}
// ==== HAS BALL HIT BOTTOM EDGE? ====
if (myBall.y > stage.stageHeight - myBall.height)
{
ud = 0;
}
// ==== HAS BALL HIT TOP EDGE? ====
if (myBall.y < 0)
{
ud = 1;
}
// ==== X AXIS MOVEMENT ====
if (lr)
{
myBall.x = myBall.x + 5; // x must be lower case
}
else
{
myBall.x = myBall.x - 5; // x must be lower case
}
// ==== Y AXIS MOVEMENT ====
if (ud)
{
myBall.y = myBall.y + 5; // y must be lower case
}
else
{
myBall.y = myBall.y - 5; // y must be lower case
}
}
Use CTRL+Enter to test the movie.
The default 12 frames per second is a bit sluggish and jerky. Try increasing this value to 24 or 30 fps.