Bouncing Ball Script

by Paulo Caldeira.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on flash  

You are here: Categories » Computers and technology » Flash

Start a new Flash movie. Create a movie clip that has a ball graphic inside it.

You can name the instance of the movie clip, myClip, but our code will not depend on the name of the clip.

Attach the following code to the movie clip:

onClipEvent(enterFrame) {
   this._x += 5;
   }

This code acts once per frame. It pushes the movie clip over by one pixel each frame. The result is a clip that moves slowly across the screen until it reaches the other side. It actually continues even past there.

To alter the code so that it bounces off the right wall, we'll need to make a few changes. The horizontal speed of the movie clip will be stored in a variable named speedX. Change the movie clip script to this:

onClipEvent(load) {
   speedX = 5;
   }
onClipEvent(enterFrame) {
   this._x += speedX;
   }

If you run the movie now, it behaves exactly as it did before. The variable speedX is set to 5, and that value is used to increment the horizontal position of the movie clip.

Now it is time to make the clip bounce off the right wall. To do this, we will test to see whether the clip's horizontal position is at, or past, the right wall. If so, speedX is reversed so that the ball moves back the way it came.

onClipEvent(load) {
   speedX = 5;
   }
   onClipEvent(enterFrame) {
   this._x += speedX;
   if (this._x >= 550) {
   speedX = -speedX;
   }
   }

Now the ball bounces off the right side of the screen and comes back toward the left wall.

To make sure that it bounces off the left wall, we'll want to test for the horizontal location of the ball being less than 0 and reverse its direction in that case too.

onClipEvent(load) {
   speedX = 5;
   }
onClipEvent(enterFrame) {
   this._x += speedX;
   if (this._x >= 550) {
   speedX = -speedX;
   }  else if (this._x <= 0) {
   speedX = -speedX;
   }
   }

Now let's make the ball move in a vertical direction as well. There is nothing new in this next alteration of code. It is just the same things we have been doing, but applied to both the horizontal and vertical directions.

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;
   }
   }

When you run the movie now, the ball bounces off all four walls. It keeps going and going. It is a good example of an animation easily done with ActionScript but next to impossible with frame-by-frame manually created animation.

You may notice that the ball seems to go slightly beyond the edges of the screen. That is because the horizontal and vertical location of the movie clip refers to the middle of the ball. If the ball is 20 pixels in diameter, the ball might appear to go about 10 pixels past the edge. You can adjust for this in your calculations in many ways. The simplest would be to use 10, 10, 540, and 390 as your screen edges, not 0, 0, 550, and 400.

Leave a comment or ask a question
Total comments: 0

Flash Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
Targeting Flash Movie Clips - The simplest way to target a movie clip is to use its name, followed by a dot, followed by the command you want to send. However, there are plenty of other ways to target a movie clip as we (more...)
Using Mathematical Operators in ActionScript - You want to modify something over time, such as the rotation or position of a movie clip. Use the compound assignment operators to change a variable or property in increments. Or, if increm (more...)
How to build an animated button using Flash - Time: 15 Minutes. Difficulty Level: Intermediate Requirements: Flash 8 Assumed Knowledge: Basic action (more...)
Enhancing Standalone Projector - You want to create an enhanced Standalone Projector with features such as borderless playback, custom titles, no Flash menus, and so on. Use a third-party tool such as SWF Studio or SWFKit to c (more...)
Creating Flash Buttons - Buttons are one of the three main types of symbols that you use in Flash. The others are movie clips and graphics. Making a New Button There are many ways to create a button. One (more...)
ActionScript: Creating Reusable Code - You want to perform a series of actions at various times without duplicating code unnecessarily throughout your movie. Create a function and then call (i.e., invoke) it by name whenever you (more...)
3D Scaling with ActionScript - Although Flash is not capable of real 3D graphics, the kind seen in popular computer games, you can create the illusion of 3D by using scaling. Scaling an object is a great way to give your (more...)
The Dot Syntax - Something else that you will be seeing a lot of as you learn ActionScript is dot syntax. Dot syntax is a way of grouping objects and functions that is used in many object-oriented programming (more...)
Movie Clip Scripts - Movie clip scripts, like button scripts, use handlers. Instead of using the on keyword that button scripts use, we will use the onClipEvent keyword. Here is an example of a movie clip script: (more...)
Decoding an RGB Value - You want to extract the red, green, and blue components from an RGB value returned by Color.getRGB( ). Use the bitshift right and bitwise AND operators. You can extract the red, gre (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.