ActionScript错误

时间:2014-05-30 03:10:30

标签: actionscript-3 button actionscript actionscript-2

我按下按钮时按下它会调用一个函数,它会显示错误的答案文本或正确的答案文本,这对我来说似乎都是正确的,但我有这个讨厌的错误。 1084:语法错误:在Button之前期待rightparen。我无法弄清楚问题出在哪里。

这是我的代码。我将不胜感激任何帮助。谢谢。

    stop();

myWelcome.text = "Hello, " + myName;

btn81.addEventListener(MouseEvent.MOUSE_UP,81Button){
    function 81Button (evt:Event):void{
    wrongAnswer();
}
}



btn85.addEventListener(MouseEvent.MOUSE_UP,85Button){
    function 81Button (evt:Event):void{
    wrongAnswer();
}
}


btn91.addEventListener(MouseEvent.MOUSE_UP,91Button){
    function 91Button (evt:Event):void{
    rightAnswer();
}
}



btn95.addEventListener(MouseEvent.MOUSE_UP,95Button){
    function 81Button (evt:Event):void{
    wrongAnswer();
}
}




function wrongAnswer (evt:Event):void{
    feedback.text = "wrong";
    noSound.play();
}
function yesSound (evt:Event):void{
    feedback.text = "Correct";
    yesSound.play();
}

2 个答案:

答案 0 :(得分:1)

您也可以使用匿名函数。

例如你的代码:

btn81.addEventListener(MouseEvent.MOUSE_UP,81Button){
    function 81Button (evt:Event):void{
    wrongAnswer();
}
}

可以转换为:

btn81.addEventListener(MouseEvent.MOUSE_UP, function(event:Event):void {
    wrongAnswer();
});

答案 1 :(得分:0)

首先,您需要更改代码,看起来很复杂。

  • 函数名称不应以数字更改81Button到button81开始。
  • 使用mouseDown而不是mouseUp。

我为你改变了代码

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       creationComplete="init(event)">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            function button81 (evt:MouseEvent):void
            {
                wrongAnswer();
            }


            function button85 (evt:MouseEvent):void
            {
                rightAnswer();
            }



            function button91 (evt:MouseEvent):void
            {
                wrongAnswer();
            }

            function button95 (evt:MouseEvent):void
            {
                rightAnswer();
            }

            private function wrongAnswer():void
            {
                trace('Wrong');
            }

            private function rightAnswer():void
            {
                trace('Right');
            }

            protected function init(event:FlexEvent):void // creation complete
            {
                btn81.addEventListener(MouseEvent.MOUSE_UP,button81);
                btn85.addEventListener(MouseEvent.MOUSE_UP,button85);
                btn91.addEventListener(MouseEvent.MOUSE_UP,button91);
                btn95.addEventListener(MouseEvent.MOUSE_UP,button95);

            }

        ]]>
    </fx:Script>
    <s:HGroup>
        <s:Button id="btn81" label="81"/>
        <s:Button id="btn85" label="85"/>
        <s:Button id="btn91" label="91"/>
        <s:Button id="btn95" label="95"/>
    </s:HGroup>
</s:WindowedApplication>