-2

I'm using this code to move an image when you press W, A, S, and D.

package
{
    import com.adobe.tvsdk.mediacore.CustomRangeType;
    import flash.display.Bitmap;
    import flash.display.BitmapEncodingColorSpace;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.events.AsyncErrorEvent;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.NetStatusEvent;
    import flash.events.TimerEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.system.ImageDecodingPolicy;
    import flash.ui.Keyboard;
    import flash.utils.ByteArray;
    import flash.utils.Timer;

    public class Main extends Sprite {

        public var xcoord:int = 50;
        public var ycoord:int = 50;
        public var movingUp:Boolean;
        public var movingDown:Boolean;
        public var movingLeft:Boolean;
        public var movingRight:Boolean;
        public var onFrontPage:Boolean;
        public var onDiner:Boolean;
        public var clock:Timer = new Timer(15, 0);
        // Image Initializations
        public var player:Bitmap = new Assets.hariidle1 as Bitmap;

        public var dinerBackground:Bitmap = new Assets.diner as Bitmap;
        public var hariIdle1:Bitmap = new Assets.hariidle1 as Bitmap;
        public var hariIdle2:Bitmap = new Assets.hariidle2 as Bitmap;

        public function Main():void {       
            if (stage){
                init();
            } else {
                addEventListener(Event.ADDED_TO_STAGE, init);   
            }

            // Set booleans to true

            onFrontPage = true;


            addChild(Assets.video);

            var nc:NetConnection = new NetConnection();
            nc.addEventListener(NetStatusEvent.NET_STATUS , onConnect);
            nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR , trace);


            var metaSniffer:Object=new Object();  
            nc.client=metaSniffer;
            metaSniffer.onMetaData=getMeta;
            nc.connect(null);

        } 
        public function init(e:Event = null):void {
            stage.removeEventListener(Event.ADDED_TO_STAGE, init);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
            clock.addEventListener(TimerEvent.TIMER, gameLoop); 
            clock.start();

        }
        private function asyncErrorHandler(event:AsyncErrorEvent):void { 
            // ignore error 
        }
        private function getMeta (mdata:Object):void {
            Assets.video.width=1280;
            Assets.video.height=720;
        };

        private function onConnect(e:NetStatusEvent):void {
            if (e.info.code == 'NetConnection.Connect.Success') {
                trace(e.target as NetConnection);
                var ns:NetStream = new NetStream(e.target as NetConnection);

                ns.client = {};
                var file:ByteArray = new Assets.bytes();
                ns.play(null);

                ns.appendBytes(file);
                Assets.video.attachNetStream(ns);
            }

        }
        private function gameLoop(evt:Event):void {


                // Draw diner background
                if (!onFrontPage && !onDiner){
                    onDiner = true;
                    addChild(dinerBackground);
                    addChild(player);
                }
                if (!onFrontPage){
                    removeChild(Assets.video);
                }
                if (movingUp){
                    player.y += 50;
                    moveUp();
                }
                if (movingDown){
                    player.y -= 50;
                    moveDown();
                }
                if (movingLeft){
                    player.x += 50;
                    moveLeft();
                }
                if (movingRight){
                    player.x -= 50;
                    moveRight();
                }               
        }
        public function moveUp():void {
            player.y += 50;
        }
        public function moveDown():void {
            player.y -= 50;
        }
        public function moveLeft():void {
            player.x += 50;
        }       
        public function moveRight():void {
            player.x -= 50;
        }
        public function onKeyDown(e:KeyboardEvent):void {
            if (onFrontPage){
                onFrontPage = false;
            }
            if (e.keyCode == 87){
                movingUp = true;
            }
            if (e.keyCode == 83){
                movingDown = true;
            }
            if (e.keyCode == 65){
                movingLeft = true;
            }
            if (e.keyCode == 68){
                movingRight = true;
            }
        }
        public function onKeyUp(e:KeyboardEvent):void {
            if (e.keyCode == 87){
                movingUp = false;
            }
            if (e.keyCode == 83){
                movingDown = false;
            }
            if (e.keyCode == 65){
                movingLeft = false;
            }
            if (e.keyCode == 68){
                movingRight = false;
            }   
        }
    }

}

There is another class called Assets that has all of the images initialized. Upon pressing W, A, S, and D, the image "player" is not moving.

How can I get the image to move when you press the arrow keys?

EDIT: I have changed the code to what was suggested. The character still does not move.

1 Answers1

0

Your flags are all messed up. Your code should set them to false on a keyUP event, and to true on a keydown event. Also you're adding TWO listeners on keydown, one sets flags to true and one to false. Make up your mind!

As a side note, no need to call addChild() in a game loop, these should be handled in listeners for semi-permanent objects like players.

And finally, you're calling stage in the constructor, hitting "Stage is unavailable" and making your listeners not work altogether. You should use this code as your ONLY code in Main():

public function Main():void {           
    if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init);
} 

And your init function should start like this:

public function init(e:Event=null):void {
   removeEventListener(Event.ADDED_TO_STAGE, init);

... and then contain all the code in your Main. Once you do all THIS, you might get your movement work.

Finally, NEVER do cargo coding, and ALWAYS think what your code does and when it is called BEFORE you're even hitting Compile.

Graham
  • 7,431
  • 18
  • 59
  • 84
Vesper
  • 18,599
  • 6
  • 39
  • 61