ActionScript Bouncing Ball
To create a ball that bounces around the edges of the window you need to use the enterFrame event. You will need to create a movie clip on the stage in exactly the same way as the ActionScript follow mouse example. Remove the code that is present in the movie clip object and put this code in its place.
onClipEvent(load){
speedX = 5;
speedY = 5;
}
onClipEvent(enterFrame){
this._x += speedX;
this._y += speedY;
if(this._x >= 550){
speedX = -speedX;
}else if(this._x <= 0){
speedX = -speedX;
}
if(this._y >= 400){
speedY = -speedY;
}else if(this._y <= 0){
speedY = -speedY;
}
}
The first part of the script sets up two variables (speedY and speedX) when the flash program loads. The rest of the script keeps the ball from going outside the confines of the frame.
Write a comment