0

我正在做一个项目,其中一个角色在屏幕上向前或向后走或射击步枪,具体取决于用户是否按下以下键盘键之一:前进箭头(键 39)、后退箭头(键 37)或空格键(关键字 32)。

我的问题是,当用户按住向前箭头键时,角色的影片剪辑会播放一个向前走动画的实例,然后向前移动。我希望在角色移动的整个过程中播放前进动画。

这是我的代码:

import fl.transitions.Tween;
import fl.transitions.easing.*;

stage.addEventListener(KeyboardEvent.KEY_DOWN, moveCharacter);
stage.addEventListener(KeyboardEvent.KEY_UP, stopCharacter);

var muzzlePosition:Number = new Number();
var bullet:Bullet = new Bullet();

function moveCharacter(e:KeyboardEvent):void {
switch (e.keyCode) {
    case 39 :
        if (sprite_Cicada.x<stage.stageWidth-150) {
            sprite_Cicada.gotoAndPlay("walk-fwd");
            sprite_Cicada.x+=5;
        } else {
            sprite_Cicada.x+=0;
            sprite_Cicada.gotoAndPlay("push");
        }
        break;

    case 37 :
        if (sprite_Cicada.x>225) {
            sprite_Cicada.x-=3;
            sprite_Cicada.gotoAndPlay("walk-bkwds");
        } else {
            sprite_Cicada.x-=0;
            sprite_Cicada.gotoAndPlay("standing");
        }
        break;

    case 32 :
        muzzlePosition=sprite_Cicada.x+sprite_Cicada.AK47.x+28;
        addChild(bullet);
        bullet.gotoAndStop("lead");
        bullet.x=muzzlePosition;
        bullet.y=328;
        sprite_Cicada.gotoAndPlay("fireAK");
        var shootBullet:Tween=new Tween(bullet,"x",None.easeOut,muzzlePosition,stage.stageWidth*2,.5,true);
        if (bullet.x>stage.stageWidth+50) {
            removeChild(bullet);
        }
        break;
    }
}

function stopCharacter(e:KeyboardEvent):void {
sprite_Cicada.gotoAndPlay("standing");
}
4

1 回答 1

0

也许你可以做

stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown );
stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
addEventListener(Event.ENTER_FRAME, movePerson);
function keydown(e:KeyboardEvent) {
        if (e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.RIGHT) { per_mc.gotoAndStop(2) }
        if (e.keyCode == Keyboard.LEFT) {leftkeyStatus = true; rightkeyStatus = false;}
        if (e.keyCode == Keyboard.RIGHT) {leftkeyStatus = false; rightkeyStatus = true;}
    }
function keyup(e:KeyboardEvent) { 
        if (e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.RIGHT) { per_mc.gotoAndStop(1) }
        if (e.keyCode == (Keyboard.LEFT)) {leftkeyStatus = false;}
        if (e.keyCode == (Keyboard.RIGHT)) {rightkeyStatus = false;}
    }

    function movePerson(e:Event) {
        if (rightkeyStatus) {
    sprite_Cicada.x-=3;
        }           
    }
于 2013-11-13T04:02:55.027 回答