The value of object variables in ActionScript.

The previous code for Ball.as could be modified to make the ball bounce off of any wall on any size stage. But you must manually go in and change the code if either the ball or stage is a different size than originally set.

Isn't there a better way to make a more general solution that would work for:

Any Movie Clip object on the stage has the properties:   The stage itself has the properties:
  • _x
  • _y
  • _rotation ( in degrees )
  • _width
  • _height
  • _xscale (%)
  • _yscale (%)
  • _alpha (transparency)
  • _visible (true or false)
 
  • width
  • height

How can these be used to create a script that will make a ball of any size bounce off the edges of a stage of any size?

The ball bounces when:

  1. it hits the right side (this._x >= Stage.width -( this._width/2 ))
  2. it hits the left side (this._x < =(this._width /2)
  3. it hits the bottom (this._y >= Stage.height - (this._height/2))
  4. it hits the top

 

Try changing the code for the Ball class and run the scene with a variety of balls of different sizes.

Note: If you use Ctrl-Enter to test the movie you MUST resize the test window (hit the resize icon at the top right of the screen before you play the movie, then Ctrl-Enter) otherwise the "stage" size will be calculated as the test window size (which includes areas out of view)

Variation:

The ball speeds up if it hits the top and slows if it hits the bottom wall (Hint: use a variable to control the distance moved)

Think of a variation of your own!