ThomasMcDonald
ThomasMcDonald

Reputation: 323

ActionScript Errors

Im making buttons so when it is pressed it will call a function that will present either a wrong answer text or a right answer text, it all seems correct to me but i have this pesky error. 1084: Syntax error: expecting rightparen before Button. I cant figure out where the problem is.

Here is my code. I would appreciate any and all help. Thank you.

    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();
}

Upvotes: 0

Views: 35

Answers (2)

Petr Hrehorovsky
Petr Hrehorovsky

Reputation: 1153

You can also use anonymous functions.

For example your code:

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

can be converted to:

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

Upvotes: 1

subrat71
subrat71

Reputation: 348

First of all you need to change your code it looks little complex.

  • Function name should not start with number change 81Button to button81.
  • use mouseDown instead of mouseUp.

i change the code for you

<?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>

Upvotes: 0

Related Questions