如果是我,我會做這樣的事情在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.distance()
用於計算飛船和敵人可能之間的距離用於計算物體的運動如Vector3D.scaleBy()
許多有用的性質用於武器的準確性或與距離有關的傷害。
來源
2017-02-01 11:28:59
Zze
這是使用Flash內的關鍵幀本身我AS2代碼:if(Key.isDown(Key.SPACE)){ this.gotoAndStop( 「20」); } \t else { this.gotoAndStop(「idle」); } \t \t if(Key.isDown(Key.RIGHT)){this._x + = 5; this.gotoAndStop(「6」); } if(Key.isDown(Key.SPACE))this.gotoAndStop(「20」); (Key.isDown(Key.LEFT)){this._x - = 5;};}} {0} {0}}如果(Key.isDown(Key.LEFT)){ } if this.gotoAndStop(「6」); } 等等...... –
您是否嘗試使用'&&'運算符來鏈接兩個條件?就像'if(event.keyCode == 40 && event.keyCode == 45){做些什麼; }用正確的鍵碼代替左箭頭鍵和空鍵鍵來替換40和45。同時仔細檢查正確的AS3語法(您沒有顯示AS3代碼,我們可以幫助修復)... –
@ VC.One我猜測他們還沒有了解eventlisteners。 –