2012-08-27 37 views
0

第1次使用的KeyboardEvent的事件監聽,我不能讓它工作:用「如果」給錯誤AS3的KeyboardEvent

下面是代碼:

stage.addEventListener(KeyboardEvent.KEY_DOWN, Cal); 

function Cal(event:KeyboardEvent):void 
{ 
    if (event.keyCode == Keyboard.ENTER) { 
     Calco(); 
    } 
} 

的錯誤,我得到的是:

場景1,圖層'操作',第1幀,第95行1136:參數的數量不正確。預計1.

線94是如果線和95卡爾科的事情... 我沒有得到問題的地方。我有另一個可以正常工作的代碼示例,我用它作爲示例。

+3

event.keyCode :) –

+0

和聽起來像坎諾瓦期待你不是在傳遞一個參數 – Allan

+0

論證這意味着我必須把東西英寸bracets? –

回答

1

一些想法...和工作示例:

import flash.events.KeyboardEvent; 
import flash.ui.Keyboard; 


// #1 - use the proper variables/function names, upper and lower cases: 
// Classes: Xxxxxx 
// function : xxxxxx, xxxXxxxx 
// variables : xxxxXxxx, xxxxx 
// constants : XXXXXX 
// #2 - use the readable function name, no shotcuts... (just few advices); 

stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyboardDown); 

function handleKeyboardDown( e :KeyboardEvent):void 
{ 
    // #3 - for keyboard events use switch instead... 

    // The error which was here is that the variable which parsed is named 'event' and you are using 'e', 
    // you need to use the same name as it being initialized 
    switch(e) 
    { 
     case Keyboard.ENTER : 

      // the error for your 'Calco()' function, is because 
      // function Calco (value.... expects for an variable. 
        // in this case this function does not required any variables 
      calculate(); 
      break; 
    } 
} 

function calculate():void 
{ 
    trace (this); 
} 
+0

感謝您的所有建議!我有一個Calco功能,它不需要給任何價值或任何東西...... –