使用双键按下动画英雄动画as3 Flash Pro CS6

时间:2017-01-31 01:52:55

标签: actionscript-3 key

我正在创造一个太空射击游戏。我试图找出当同时按下空格键和方向键时如何编码/运行我的影片剪辑(枪口闪光)。

这是我在AS2代码中使用Flash本身的关键帧:

if(Key.isDown(Key.SPACE)){
    this.gotoAndStop("20");
} else {
    this.gotoAndStop("idle");
}

if(Key.isDown(Key.RIGHT)){
    this._x += 5;
    this.gotoAndStop("6");
}

if(Key.isDown(Key.SPACE)){
    this.gotoAndStop("20");
}

if(Key.isDown(Key.LEFT)){
    this._x -= 5;
    this.gotoAndStop("6");
}

and so on...

1 个答案:

答案 0 :(得分:1)

如果这是我,我会在AS3中做这样的事情:

stop();

var velocity: Vector3D = new Vector3D(0,0,0);
var shooting: Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, function(evt: KeyboardEvent){
    // have we moved on the X axis?
    velocity.x = evt.keyCode == 37 ? -1: evt.keyCode == 39 ? 1: velocity.x;
    // have we moved on the Y axis?
    velocity.y = evt.keyCode == 40 ? -1: evt.keyCode == 38 ? 1: velocity.y;
    // Have we shot?
    shooting = evt.keyCode == 32 ? true : shooting;
});

stage.addEventListener(KeyboardEvent.KEY_UP, function(evt: KeyboardEvent){
    // Have we finished moving on the X axis?
    velocity.x = evt.keyCode == 37 || 39 ? 0 : velocity.x;
    // Have we finished moving on the Y axis?
    velocity.y = evt.keyCode == 40 || 38 ? 0 : velocity.y;
    // have we finished shooting?
    shooting = evt.keyCode == 32 ? false : shooting;
});

stage.addEventListener(Event.EXIT_FRAME, function(evt: Event){
    // evaluate velocity and shooting and jump to the required keyframes.
    trace(velocity, shooting);
});

关键是评估在两个Keyboard event listeners中按下了哪个键,然后在帧的末尾,然后根据已收集的所有数据更新动画片段。我认为这很重要,因为你知道当宇宙飞船最终移动时,它肯定会处于最新的状态。

我还使用Vector3D来存储宇宙飞船的速度,因为它有许多有用的属性来计算物体的移动,例如Vector3D.scaleBy()用于向航天器施加速度,Vector3D.distance()用于计算宇宙飞船与敌人之间的距离,该距离可用于武器准确性或与距离有关的伤害。