as3多个key_down移动对象

时间:2014-01-01 15:27:13

标签: actionscript-3

当KEY_DOWN发生等待你的评论时,我的对象“araba”没有移动!     

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.display.Bitmap;

stage.addEventListener(KeyboardEvent.KEY_DOWN, basili); stage.addEventListener(KeyboardEvent.KEY_UP, degil); stage.addEventListener(Event.ENTER_FRAME, giris); var yukari:Boolean = false; var asagi:Boolean = false; var sola:Boolean = false; var saga:Boolean = false; var hiz:Number = 5; var araba:MovieClip=new araba(); var masa:MovieClip =new masa(); function giris(event:Event):void { if ( sola && !saga ) { araba.x -= hiz; araba.rotation = 270; } if( saga && !sola ) { araba.x +=hiz; araba.rotation = 90; } if( yukari && !asagi ) { araba.y -= hiz; araba.rotation = 0; } if( asagi && !yukari ) { araba.y += hiz; araba.rotation = 180; } if( sola && yukari && !saga && !asagi ) { araba.rotation = 315; } if( saga && yukari && !sola && !asagi ) { araba.rotation = 45; } if( sola && asagi && !saga && !yukari ) { araba.rotation = 225; } if( saga && asagi && !sola && !yukari) { araba.rotation = 135; } if( araba.y < masa.y ){ araba.y = masa.height; } if( araba.y > masa.height ){ araba.y = masa.y; } if( araba.x < masa.x ){ araba.x = masa.width; } if( araba.x > masa.width ){ araba.x = masa.x; } } function basili (event:KeyboardEvent):void { switch( event.keyCode ) { case Keyboard.UP: yukari = true; break; case Keyboard.DOWN: asagi = true; break; case Keyboard.LEFT: sola = true; break; case Keyboard.RIGHT: saga = true; break; } } function degil(event:KeyboardEvent):void { switch( event.keyCode ) { case Keyboard.UP: yukari = false; break; case Keyboard.DOWN: asagi = false; break; case Keyboard.LEFT: sola = false; break; case Keyboard.RIGHT: saga = false; break; } } <code>

2 个答案:

答案 0 :(得分:0)

我看过你的代码,它看起来很好,但你为什么不尝试追踪,看看会发生什么?

答案 1 :(得分:0)

        private var leftKeyIsDown:Boolean;
        private var rightKeyIsdown:Boolean
        private var downKeyIsDown:Boolean;
        private var upKeyIsDown:Boolean;





        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown); //when key is down, go to this function
        stage.addEventListener(KeyboardEvent.KEY_UP, keyUp); //when key is up, go to this function
        gameloop();

        private function gameLoop(e:Event):void //this function runs 30 times every second, due to 30fps
        {
        playerControl();
        }

        private function keyDown(e:KeyboardEvent):void 
        {
                //trace Event and KeyCode, when we trace this we get I.D number from key
                //trace(e.keyCode);
                if (e.keyCode == 37)
                {
                    //left key is pressed
                    leftKeyIsDown = true;
                }
                if (e.keyCode == 38)
                {
                    //up key is pressed
                    upKeyIsDown = true;
                }
                if (e.keyCode == 39)
                {
                    //right key is pressed
                    rightKeyIsdown = true;
                }
                if (e.keyCode == 40)
                {
                    //down key is pressed
                    downKeyIsDown = true;
                }

        }

    private function keyUp(e:KeyboardEvent):void 
    {
        //if our left key is released
        if (e.keyCode == 37)
        {
            //left key is released
            leftKeyIsDown = false;
        }
        if (e.keyCode == 38)
        {
            //up key is released
            upKeyIsDown = false;
        }
        if (e.keyCode == 39)
        {
            //right key is released
            rightKeyIsdown = false;
        }
        if (e.keyCode == 40)
        {
            //down key is released
            downKeyIsDown = false;
        }


    }

    private function playerControl():void 
    {
        //if left is currently down
        if (leftKeyIsDown)
        {
            //move ship to the left
            ship.x -= 5;  //subtract 5 from ships position evey loop, 

        }   

        //if right is currently down
        if (rightKeyIsdown)
        {
            //move ship to the right
            ship.x += 5;  //subtract 5 from ships position evey loop, 
        }   

        //if up is currently down
        if (upKeyIsDown)
        {
            //move ship up
            ship.y -= 5;  //subtract 5 from ships position evey loop, 
        }   

        //if left is currently down
        if (downKeyIsDown)
        {
            //move ship down
            ship.y += 5;  //subtract 5 from ships position evey loop, 
        }   
    }

用您的物品替换船只。

这是一个模板,我也评论过。

请告诉我这是否有用,它不会修复您的代码,我知道。

但希望你能适应它。