Kyle93
Kyle93

Reputation: 795

Adding shape to stage as3

I'm not sure if I'm over or under thinking this however I've got this scenario. I wish to be able to add (for now) a triangle to the page using multiple classes. The first one being adding actionscript to a frame in flash the second an actual class. Now, can I do this? Or am I silly thinking I can? I wish to do this so I can create my background as a dynamically created area where thinks I can interact with are placed on as instances of a class.

On frame

import flash.display.Stage;

var sides:Sides=new Sides();
stage.addChild(sides);
this.addChild(sides);

On class

package  {
    import flash.display.Shape;

        public class Sides extends Shape {
            public function Sides() {
                var triangleHeight:uint = 100; 
            var triangle:Shape = new Shape(); 

            // red triangle, starting at point 0, 0 
            triangle.graphics.beginFill(0xFF0000); 
            triangle.graphics.moveTo(triangleHeight / 2, 0); 
            triangle.graphics.lineTo(triangleHeight, triangleHeight); 
            triangle.graphics.lineTo(0, triangleHeight); 
            triangle.graphics.lineTo(triangleHeight / 2, 0);
            triangle.graphics.endFill();
            trace("Into construct");
        }
    }
}

The issue I have is that the actual triangle does not appear on the screen it's blank. I know the constructor is ran, however I get no actual output as such.

I hope I made myself clear. If anyone can suggest a better solution I would love to hear it. My scenario is this. I wish to create a world that other movie-clips can interact with. I will be creating lines to represent them. Now is it better to do it dynamically generated or is there a way to have some sort of base class that all of the other ones run off where that allows me to have random width. Hope this is clear.

Upvotes: 0

Views: 617

Answers (1)

milks
milks

Reputation: 314

you create an instance of a Shape within your Sides constructor to which you draw the triangle however this shape is never added to a display list, instead you add your instance of Sides (which itself has nothing drawn) to a display list.

Because your Sides class is extending Shape you don't need another instance of a Shape, your instance of Sides itself is a Shape and you can draw directly to it like so:

package  {
    import flash.display.Shape;

        public class Sides extends Shape {
            public function Sides() {
                var triangleHeight:uint = 100;

                // red triangle, starting at point 0, 0 
                this.graphics.beginFill(0xFF0000); 
                this.graphics.moveTo(triangleHeight / 2, 0); 
                this.graphics.lineTo(triangleHeight, triangleHeight); 
                this.graphics.lineTo(0, triangleHeight); 
                this.graphics.lineTo(triangleHeight / 2, 0);
                this.graphics.endFill();
                trace("Into construct");
        }
    }
}

Upvotes: 1

Related Questions