运动不工作AS3

时间:2016-06-11 01:05:12

标签: actionscript-3

我目前正在开发一款游戏,而且对AS3来说相当新。

我坚持角色运动:我正在关注指南并最终得到以下作为我的代码。当我测试游戏时,它只是播放角色动画而我无法控制它。

    package {

    import flash.display.Stage;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.utils.Proxy;
    import flash.utils.flash_proxy;

    dynamic public class KeyObject extends Proxy {

        private static var stage:Stage;
        private static var keysDown:Object;

        public function KeyObject(stage:Stage) {
            construct(stage);
        }

        public function construct(stage:Stage):void {
            KeyObject.stage = stage;
            keysDown = new Object();
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
        }

        flash_proxy override function getProperty(name:*):* {
            return (name in Keyboard) ? Keyboard[name] : -1;
        }

        public function isDown(keyCode:uint):Boolean {
            return Boolean(keyCode in keysDown);
        }

        public function deconstruct():void {
            stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
            stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
            keysDown = new Object();
            KeyObject.stage = null;
        }

        private function keyPressed(evt:KeyboardEvent):void {
            keysDown[evt.keyCode] = true;
        }

        private function keyReleased(evt:KeyboardEvent):void {
            delete keysDown[evt.keyCode];
        }
    }
}
package  {
    import flash.display.Sprite
    import flash.events.Event;
    import KeyObject;
    public class Main extends Sprite{
        private var key:KeyObject;

        public function Main() {
            addEventListener(Event.ADDED_TO_STAGE,setupKeyObject);
        }

        function setupKeyObject(e:Event){
            key = new KeyObject(stage);
            stage.addEventListener(Event.ENTER_FRAME,movePlayer);
        }

        function movePlayer(e:Event){
            if(key.isDown(key.LEFT)){
               roy.x -= 5;
               }
            if(key.isDown(key.RIGHT)){
               roy.x +=5;
               }              
            if(roy.x<0){
                roy.x = 0;
            }
            if(roy.x > (stage.stageWidth - player.width)){
                roy.x = stage.stageWidth - player.width;
            }
        }
    }
}

0 个答案:

没有答案