views:

39

answers:

2

Say i have these two classes:

MAIN.as

package 
{
    import flash.display.*;     import mx.core.*;       
    import flash.events.*;      import mx.collections.*;
    import flash.geom.*;        import mx.controls.*;
    import flash.text.*;        import mx.events.*;
                                import mx.styles.*;
                                import mx.containers.*;

    public class MAIN extends Sprite
    {
        public var APPLICATION:Application = Application(Application.application);
        public var keyDownText:TextField = new TextField();
        public function MAIN()
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN,KEY_DOWN);
            addEventListener(Event.ENTER_FRAME,STEP);
            new OBJECT_square().CREATE(10,100,1);
        }

        public function STEP():void {}
        public function DRAW():void {}
        public function KEY_DOWN(event:KeyboardEvent):void 
        {
            keyDownText.text = "Key code: " + event.keyCode;
            this.addChild(keyDownText);
        }
    }
}

OBJECT_square.as

package
{
    import flash.display.*;
    import flash.events.*;

    public class OBJECT_square extends Sprite
    {
        public var X:int = 0;
        public var Y:int = 0;
        public var DEPTH:int = 0;
        public var SPRITE:Sprite = new Sprite();
        public function CREATE(X:int,Y:int,DEPTH:int):void
        {
            this.DEPTH = DEPTH;
            this.X = X;
            this.Y = Y;
            DRAW();
        }
        public function DRAW():void
        {
            SPRITE.graphics.beginFill(0xFF0000,1);
            SPRITE.graphics.drawRect(X - 10,Y - 10,20,20);
            SPRITE.graphics.endFill();
            addChild(SPRITE);
        }
    }
}

Now how is it that I can add the variable SPRITE which is a Sprite in the OBJECT_square class to the display list of MAIN class? I've tried addChild(SPRITE) and super.addChild(SPRITE). If everything works I should see a red square somewhere on the screen but right now its all blank, except for the text drawn in the MAIN class.

Basically I want it so i can just make a new OBJECT_square and it will draw itself without any more instructions from the MAIN class.

+1  A: 

Try this:

var obj:OBJECT_square = new OBJECT_square();
obj.CREATE(10,100,1);
addChild(obj);

Or, if you really want to do it in one go, you could try this:

In main

addChild((new OBJECT_square()).CREATE(10,100,1));

And change your draw function to return the square object

    public function DRAW():OBJECT_square
    {
        SPRITE.graphics.beginFill(0xFF0000,1);
        SPRITE.graphics.drawRect(X - 10,Y - 10,20,20);
        SPRITE.graphics.endFill();
        addChild(SPRITE);
        return this;
    }
Juan Pablo Califano
+1 because he didn't give it to you. So, he's on his fourth account now?
Gunslinger47
@Gunslinger47. More like the tenth. But the style is consistently... unique?
Juan Pablo Califano
A: 

I used the second example your provided because I don't want to go through the trouble of naming every instance of OBJECT_square. It draws the square just fine, but lets say I want to move the square (change OBJECT_square's X and Y values). How would I do that? I've tried making this modification to the OBJECT_square class but no luck:

package
{
    import flash.display.*;
    import flash.events.*;

    public class OBJECT_square extends Sprite
    {
        public var X:int = 0;
        public var Y:int = 0;
        public var DEPTH:int = 0;
        public var SPRITE:Sprite = new Sprite();
        public function CREATE(X:int,Y:int,DEPTH:int):OBJECT_square
        {
            addEventListener(KeyboardEvent.KEY_DOWN,KEY_DOWN);
            this.DEPTH = DEPTH;
            this.X = X;
            this.Y = Y;
            return DRAW();
        }
        public function DRAW():OBJECT_square
        {
            SPRITE.graphics.beginFill(0xFF0000,1);
            SPRITE.graphics.drawRect(X - 10,Y - 10,20,20);
            SPRITE.graphics.endFill();
            addChild(SPRITE);
            return this;
        }
        public function KEY_DOWN(event:KeyboardEvent):void 
        {
            X += 3;
            SPRITE.graphics.clear();
            DRAW();
        }
    }
}
1101