<< Chapter < Page Chapter >> Page >

Completion of the constructor for the Main class

Listing 6 completes the constructor for the Main class.

Completion of the constructor for the main class.

moveableImage = new MoveableImage(rectWidth,rectHeight);addChild(moveableImage);//default location is 0,0addEventListener(Event.ENTER_FRAME, onEnterFrame); }//end constructor

A new object of a custom class

Listing 6 begins by instantiating an object of the new custom class named MoveableImage , passing the width and height of the rectangle to the constructor for that class.

Then Listing 6 adds the new object to the display list. This will cause it to be rendered in the Flash Player window during the next display frame.

Finally, Listing 6 registers an event handler for ENTER_FRAME events.

The ENTER_FRAME event handler

The event handler is shown in Listing 7. Each time the onEnterFrame method is called, a message is sent to the MoveableImage object asking it to execute its moveIt method.

The enter_frame event handler.

public function onEnterFrame(event:Event):void { //Ask the image to move.moveableImage.moveIt(); }//end onEnterFrame}//end class}}//end package

The end of the Main class

Listing 7 also signals the end of the Main class.

Beginning of the MoveableImage class

A complete listing of the MoveableImage class is provided in Listing 20. The beginning of the MoveableImage class is shown in the code fragment in Listing 8.

Beginning of the moveableimage class for the project named animation07.

package { import flash.display.Sprite;import flash.events.Event; import flash.display.Bitmap;public class MoveableImage extends Sprite{private var dx:Number = 4;//x-movement distance private var dy:Number = 2;//y-movement distanceprivate var rectWidth:uint;private var rectHeight:uint; private var imageWidth:uint;private var imageHeight:uint;

There is nothing new in Listing 8.

Embed the image in the swf file

The constructor for the MoveableImage class continues in Listing 9. The code in Listing 9 extracts an image from the specified image fileand embeds it in the swf file with a reference named headImage .

Embed the image in the swf file.

[Embed(source='/baldwin.jpg')] private var imgClass:Class;private var headImage:Bitmap = new imgClass ();

New to this lesson

I'm not going to try to explain how and why it works. I will simplysuggest that you memorize the syntax for the next time that you need to do the same thing.

In addition, I will refer you to the following website where you will find an explanation.

The constructor for the MoveableImage class

The constructor for the class is shown in its entirety in Listing 10.

The constructor for the moveableimage class.

public function MoveableImage(rectWidth:uint,rectHeight:uint) { //Save the dimensions of the rectangle.this.rectWidth = rectWidth; this.rectHeight = rectHeight;//Get and save the dimensions of the image.

The first two statements in Listing 10 simply save the width and height of the rectangle for later use. That shouldn't be new to you.

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