2013-09-25 93 views
0

我想在AS3中編寫一個簡單的程序非OOP(我直接編碼到時間軸),但我的函數「選擇」不像它應該調用。實際上它並沒有被調用,我也沒有收到編譯器錯誤。這裏是我的代碼:在AS3中使用鍵盤事件

//Black jack game 
import flash.events.Event; 
import flash.ui.Mouse; 
import flash.events.KeyboardEvent; 
import flash.ui.Keyboard; 
addEventListener(Event.ENTER_FRAME,talk); 

stop(); 
var yourHand1:Number = 0; 
var cHand1:Number = 0; 
var yourHand2:Number = 0; 
var cHand2:Number = 0; 

function talk(e:Event){ 

    if(yourHand1 < 3){ 
     yourHand1 +1; 
    } 
    if(cHand1 < 3){ 
     cHand1 +1; 
    } 
    cHand1 = 1+Math.floor(Math.random() * 11); 
    yourHand1 = 1+Math.floor(Math.random()* 11); 
    trace(cHand1); 
    trace(yourHand1); 
    cHand.text = cHand1.toString(); 
    yourHand.text = yourHand1.toString(); 
    removeEventListener(Event.ENTER_FRAME, talk); 
    //choices(); 
} 

addEventListener(KeyboardEvent.KEY_DOWN,choices); 

function choices(event:KeyboardEvent){ 
    trace("Would you like to hit or stay?"); 
    trace("Press left arrow to hit, space bar to stay"); 
    if(event.charCode == 65){ 
     trace("You have chosen to stay"); 
    } 
    if(event.charCode == 66){ 
     letsDoItAllAgain(); 
    } 
} 


function letsDoItAllAgain(){ 

    if(yourHand2 <= 2){ 
     yourHand2 +2; 
    } 
    if(cHand2 <= 2){ 
     cHand2 +2; 
    } 
    cHand2 = 1+Math.floor(Math.random() * 11); 
    yourHand2 = 1+Math.floor(Math.random()* 11); 
    trace(cHand2); 
    trace(yourHand2); 
    cHand.text = (cHand2 + cHand1).toString(); 
    yourHand.text = (yourHand1 + cHand2).toString(); 
    removeEventListener(Event.ENTER_FRAME, letsDoItAllAgain); 
} 
+1

簡單的程序,這是不正確的語句,即使在時間線上,as3也是OOP語言。 – joshua

+0

如果你直接打電話,它會工作嗎?如果是這樣,那麼可以得出結論:'KEY_DOWN'事件沒有發射,這將是問題的焦點。 – puggsoy

回答

3

嘗試在舞臺上添加eventListener。

stage.addEventListener(KeyboardEvent.KEY_DOWN,choices); 
0

另外潘說:

stage.addEventListener(KeyboardEvent.KEY_DOWN,choices); 

重要的是要知道你可以失去你的主窗口的焦點。 我在我的項目(太時間軸)有些相像,我必須定期這個(當我知道的焦點可能會丟失)稱:

stage.focus = stage; 
在AS3非OOP