带有“if”的AS3 KeyboardEvent给出错误

时间:2012-08-27 12:48:04

标签: actionscript-3 keyboard-events

第一次使用EventListener为KeyboardEvent而我无法使其工作:

以下是代码:

stage.addEventListener(KeyboardEvent.KEY_DOWN, Cal);

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

我得到的错误是:

场景1,图层'动作',第1帧,第95行1136:参数数量不正确。预计1。

94行是if行,95是Calco的东西...... 我不知道问题出在哪里。我有另一个代码示例工作正常,我用它作为一个例子。

1 个答案:

答案 0 :(得分: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);
}