不論你在網路上搜尋那一篇談到as3.0鍵盤觸發的文章,作法都是大同小異,而且重點是 "無法產生組合鍵"
底下將描述如何 產生組合鍵邏輯
//請注意 底下程式碼已經簡化過 許多參數未提及,請參考邏輯觀念
var ddd:Array=new Array();
ddd=[0,0,0,0];
//看到陣列也許,許多網友已經知道要如何做了,沒錯我們就是要利用陣列
//產生的數列組合去選擇要做那一個action
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, reportKeyUp);
//分為兩個部份DOWN負責紀錄按下,陣列紀錄為1
function reportKeyDown(event:KeyboardEvent):void {
//objKeyboard.pressed=true;
//objKeyboard.keyCode=event.keyCode;
if (event.keyCode==37) {
ddd[0]=1;
}
if (event.keyCode==39) {
ddd[1]=1;
}
if (event.keyCode==38) {
ddd[2]=1;
}
if (event.keyCode==40) {
ddd[3]=1;
}
}
//UP負責紀錄按下,陣列紀錄為0
function reportKeyUp(evt:KeyboardEvent):void {
//objKeyboard.pressed=false;
//objKeyboard.keyCode=null;
if (evt.keyCode==37) {
ddd[0]=0;
}
if (evt.keyCode==39) {
ddd[1]=0;
}
if (evt.keyCode==38) {
ddd[2]=0;
}
if (evt.keyCode==40) {
ddd[3]=0;
}
}
var com_ddd:String=String(ddd[0]+""+ddd[1]+ddd[2]+ddd[3]);
//將字串組合塞到switch做選擇
switch (Number(com_ddd)) {
case 1010 :
trace("左上"); yspeed -= power*1.3;xspeed -= power*1.3;
break;
case 0110 :
trace("右上"); yspeed -= power*1.3;xspeed += power*1.3;
break;
case 1001 :
trace("左下"); yspeed += power*1.3;xspeed -= power*1.3;
break;
case 0101 :
trace("右下"); yspeed += power*1.3;xspeed += power*1.3;
break;
case 0010 :
trace("上"); yspeed -= power*1.3;
break;
case 0001 :
trace("下"); yspeed += power*1.3;
break;
case 1000 :
trace("左"); xspeed -= power*1.3;
break;
case 0100 :
trace("右"); xspeed += power*1.3;
break;
}
請參閱 KeyManager Class for Detecting Key Press Combos or Sequences
http://keith-hair.net/blog/2010/02/15/keymanager-class-for-detecting-key-press-combos-or-sequences/