使用双键按 as3 Flash Pro CS6 为英雄制作动画



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

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

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...

如果这是我,我会在 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(),可用于武器精度或相对于距离的伤害。

最新更新