2012-08-27 18 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功能,它不需要给任何价值或任何东西...... –