0

第一次将 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 的东西……我不明白问题出在哪里。我有另一个运行良好的代码示例,我以它为例。

4

1 回答 1

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);
}
于 2012-08-27T13:00:56.607 回答