<< Chapter < Page | Chapter >> Page > |
The next two statements use the reference to the embedded image ( headImage ) to get and save the width and height of the image referred to by the instance variable (see Listing 9) named headImage .
The last statement in Listing 10 adds that image to the display list as a child of the Sprite object.
The moveIt method of the MoveableImage class
The moveIt method is shown in its entirety in Listing 11.
//Cause the Sprite object to move and bounce off of
// the edges of the rectangle.public function moveIt():void {
//Test for a collision with the left or right edge// of the rectangle.
if (((this.x + dx)>(rectWidth - imageWidth)
|| (this.x<0))) {
dx *= -1;//Reverse horizontal direction}//end if//Test for a collision with the top or the bottom
// of the rectangle.if (((this.y + dy)>(rectHeight - imageHeight)
|| (this.y<0))) {
dy *= -1;//Reverse vertical direction.}//end if//Make the move.
this.x += dx;this.y += dy;
}//end onEnterFrame}//end class}}//end package
The moveIt method modifies the x and y property values of the Sprite object so that it will be rendered in a different location during the next display frame.
The logic that causes the object to bounce off the edges may take a few minutes for you to unravel. Otherwise, you should have no trouble understandingthe code in Listing 11.
The end of the class
Listing 11 signals the end of the MoveableImage class.
The code for this project is shown in its entirety in Listing 21.
This project is essentially the same as Animation01 except that this project creates a Timer object and uses TIMER events in place of ENTER_FRAME events for timing.
If you run this project, you will see that just like Animation01 , it causes a filled blue circle that is drawn on a transparent Sprite object to move diagonally from left to right across the Flash window.The sprite with the circle moves out of the Flash window at the bottom right.
Because of the similarity of this project to the Animation01 project, I am only going to discuss the code that is significantly differentbetween the two projects.
New import directives for the project named Animation01A
Listing 12 shows two new import directives that are required to make it possible to instantiate an object of the Timer class.
import flash.events.TimerEvent;
import flash.utils.Timer;
Instantiate a new Timer object
Listing 13 shows the code that instantiates a new Timer object. This object will fire a TIMER event every 30 milliseconds and will continue to fire TIMER events indefinitely.
private var timer:Timer = new Timer(30);
Register a TIMER event handler and start the timer
The code in Listing 14 registers an event handler on the Timer object and then starts the timer running.
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?