<< Chapter < Page Chapter >> Page >

Another object of type Sprite

Listing 2 instantiates another object of the Sprite class and draws a blue filled circle on that object.

Then it adds that object to the display list as a child of the object of the class Main .

Draw a filled circle

According to the documentation, the beginFill method that is called on the Sprite object in Listing 2:

"Specifies a simple one-color fill that subsequent calls to other Graphics methods (such as lineTo() or drawCircle() ) use when drawing. The fill remains in effect until you call the beginFill() , beginBitmapFill() , or beginGradientFill() method. Calling the clear() method clears the fill.The fill is not rendered until the endFill() method is called."

That should be a sufficient explanation of how the code in Listing 2 draws a filled circle on the new Sprite object.

Add the new Sprite object to the display list

As mentioned earlier, the last statement in Listing 2 adds the new Sprite object to the display list as a child of the object of the Main class. By default , the new Sprite object is added at coordinates 0,0 which is the upper-left corner of the Main object.

Register an ENTER_FRAME listener

Listing 3 registers an event handler method named onEnterFrame that will be executed each time the Flash Player enters a new display frame.

Listing 3 also signals the end of the constructor.

Register an enter_frame listener.

addEventListener(Event.ENTER_FRAME, onEnterFrame); }//end constructor

The event handler method named onEnterFrame

The event handler method named onEnterFrame is shown in its entirety in Listing 4.

The event handler method named onenterframe.

public function onEnterFrame(event:Event):void { sprite.x += dx;sprite.y += dy; }//end onEnterFrame}//end class}//end package

Properties named x and y

The Sprite object that was instantiated in Listing 2 has properties named x and y . The documentation has this to say about the property named x .

"Indicates the x coordinate of the DisplayObject instance relative to the local coordinates of the parent DisplayObjectContainer."

The description of the property named y is very similar.

What this means in practice

What this means in practice is that the Flash Player uses the values of x and y properties as coordinates to decide where to draw the Sprite object relative to the upper-left corner of its container, which in this case is the object of the Main class.

By default, these property values are zero, which explains why the new Sprite object is initially drawn at the upper-left corner of the Main object.

Modify the x and y property values

Each time the Flash Player fires an ENTER_FRAME event, the code in Listing 4 increases the values of the x and y property values of the Sprite object by the amounts assigned to dx and dy in Listing 1.

Draw the sprite in a new location

The next time the objects in the display list are rendered on the screen, the Sprite object containing the filled blue circle will be drawn a little further to the right and a little further down the screen. That is whatcauses the blue ball to move diagonally from left to right across the Flash Player window when you run the project.

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