TAGS :Viewed: 9 - Published at: a few seconds ago

[ AS3: How do i draw shapes in my own class ]

I'm very new on AS3. I would like to create a shape as i defined in my own constructor class.

It should create a shape when class was created. (Constructor)

I commented my want in following code :

ballShape class

public class ballShape {

        public function ballShape() {
            // define shape properties.
            // create shape and put that in x = 0, y = 0 


Any helps would be awesome.

Answer 1

You can easily do this while extending your class to Shape or Sprite

Here's your code

public class ballShape extends Sprite {

    public function ballShape() {
        // define shape properties. The graphics object is already added to your Sprite, no need to manually addChild() this object.
        graphics.beginFill(color, alpha); // you can begin a fill with this method, there are also methods to start a bitmap fill, gradient fill. 
        graphics.drawRect( x, y, width, height ); // draw a shape


While Shape can have the same functionality to draw shapes and lines, I chose Sprite, because:

  • You will have interactivity and be able to dispatch events from that class
  • You will have a set of useful properties that Sprite has.

For more info on the Graphics class, please refer to http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Graphics.html