簡體   English   中英

使用雙按鍵為英雄制作動畫 as3 Flash Pro CS6

[英]animating a hero with a double key press 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()用於計算飛船與飛船之間的距離可能用於武器准確性或與距離相關的傷害的敵人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM