<< Chapter < Page | Chapter >> Page > |
Completion of the constructor for the Main class
Listing 6 completes 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.
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.
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(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.
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.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?