views:

33

answers:

1

For starters let me just say that I am very new to action script and also that I'm not using adobe creative suite, I am using notepad with flex as a compiler. I have two classes, a main class and a class called OBJECT_square.

Here is the MAIN class:

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);
                this.addChild(new OBJECT_square().CREATE(10,100,1));
                this.addChild(new OBJECT_square().CREATE(10,200,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);
            }
        }
    }

Here is the OBJECT_square class:

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):Sprite
        {
            addEventListener(KeyboardEvent.KEY_DOWN,KEY_DOWN);
            this.DEPTH = DEPTH;
            this.X = X;
            this.Y = Y;
            DRAW();
            return SPRITE;
        }
        public function KEY_DOWN(event:KeyboardEvent):void 
        {
            if (event.keyCode == 39) {X += 1; DRAW();}
        }
        public function DRAW():void
        {
            SPRITE.graphics.beginFill(0xFF0000,1);
            SPRITE.graphics.drawRect(X - 10,Y - 10,20,20);
            SPRITE.graphics.endFill();
        }
    }
}

Now my problem is this. Simply put I would like the square that OBJECT_square draws to move right when I press the right arrow key. Right now I can only get it to be drawn once (though I am actually creating two separate squares in two different spots). Now the thing I need to know is if I have used the keyboard event correctly in OBJECT_square. I used keyboard event in the main class as well to display the last key I pressed but I'm not sure how to use it in a class other than the main one. Also I'm not sure how to 'update' the square so that it is redrawn when it moves, that is remove its old self from the display list and reinsert its new self. The whole kinda point to the code is that the square be 'self-sufficient', ie contains all the code needed to draw it and move it rather than relying on any other class. I basically want it so that once another class has made a square the square is capable of doing everything else on its own.

A: 

You don't need to keep manually redrawing the square. Sprites have x and y properties that you can change and will automatically be moved. There is also no reason to be creating a sprite inside a class that is already extending sprite. There are a couple of other weird things you are doing so a fixed up version of your class would look something like:

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

    public class ObjectSquare extends Sprite
    {

        public function ObjectSquare (x:int,y:int):void
        {
            graphics.beginFill(0xFF0000,1);
            graphics.drawRect(x,y,30,30);
            graphics.endFill();
            addEventListener(KeyboardEvent.KEY_DOWN,KEY_DOWN);

        }
        public function KEY_DOWN(event:KeyboardEvent):void 
        {
            if (event.keyCode == 39) 
            {
                x += 1;
            }
        }

    }
}



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.*;
       //rename your class to Main. Should not be all caps
        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,onKeyPress);
                addChild(new ObjectSquare(10,100));
                addChild(new ObjectSquare(10,200));
            }

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

You would probably be better off creating a class to handle all your keyboard inputs and then change the relevant objects.

Allan