Home > ActionScript > ActionScript Bouncing Ball

ActionScript Bouncing Ball

February 27th, 2008 Leave a comment Go to comments

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.

Information, services, and products:
We have a great choice of Window Stickers come and see.
Categories: ActionScript Tags: , ,
  1. daZe
    July 13th, 2009 at 17:07 | #1

    Thats’it?? Where is the rest? WHY would you think this would be helpful for ANYONE?

  2. hush yo Onvic
    October 25th, 2010 at 11:08 | #2

    Hey give em a break its for basic learners so hush yo mouth up

  3. bobbbbob
    November 30th, 2010 at 20:13 | #3

    @daZe
    what do you mean? he explains the rest in the link he linked to I think.
    did you mean more explanation on how it works?
    it checks if its past aboundry, and if it is, negates the speed so it bounces.
    it also moves with the speed rate.

  1. No trackbacks yet.