<< Chapter < Page Chapter >> Page >

Nothing to stop it

Since there is nothing in the code to reverse the process of incrementing the x and y property values, the blue ball continues moving until it disappears off the Flash Player window on thelower-right side.

I will do something about that in the next project, which causes an image that is a caricature of me to bounce around inside of a rectangle.

The project named Animation07

A major upgrade

This project is a major upgrade of the project named Animation01 in several respects. I recommend that you run this project before continuing.

Draw a rectangle

First, the constructor for the Main class draws a 450 x 500 pixel rectangle with a yellow background and border with a thickness of threepixels. (The constructor begins in Listing 5 and continues into Listing 6.)

An object of a new class

Then the constructor instantiates an object of a new MoveableImage class, passing the dimensions of the rectangle to the constructor for that class and adds that object to the display list.

An event handler

Finally, the constructor for the Main class registers an ENTER_FRAME event handler, which asks the MoveableImage object to move each time it is called. (See Listing 7.)

The MoveableImage class

The MoveableImage class extends the Sprite class and embeds an image in the Sprite object when it is instantiated. (See Listing 8 and Listing 9.)

The dimensions of a rectangle

The constructor for the MoveableImage class (see Listing 10) receives the dimensions of a rectangle as incoming parameters and saves those dimensions forlater use.

The moveIt method

The MoveableImage class defines a method named moveIt (see Listing 11) .

Each time the moveIt method is called, the object, (including the embedded image) moves by a prescribed distance in the horizontal and vertical directions.

Bounce off the edges

Whenever the object collides with an edge of the rectangle, it bounces off the edge and starts moving in a different direction.

Beginning of the Main class for the project named Animation07

As before, I will explain this program in fragments. A complete listing of the Main class for the program is provided in Listing 19.

The Main class begins in the fragment shown in Listing 5.

Beginning of the main class for the project named animation07.

package { import flash.display.Sprite;import flash.events.Event;public class Main extends Sprite{ private var moveableImage:MoveableImageprivate var rectWidth:uint = 450; private var rectHeight:uint = 500;public function Main() {//Draw a black rectangle with a yellow background.this.graphics.beginFill(0xFFFF00,1.0);

The only things that are new in Listing 5 are the two statements that call the lineStyle and drawRect methods of the Graphics class.

The first of the two statements sets the line style for the rectangle to be three pixels thick and to be black.

The second of the two statements sets the upper left corner of the rectangle to a coordinate position of 0,0 in the parent container and sets the width andheight to the values established earlier when the width and height variables were declared and initialized.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask