TomFrom
TomFrom

Reputation: 99

AS3 / Flash - Add event listener in custom class of a button - Error 1061

I'm getting an error whilst trying to add an event listener to a class of a Button on my stage.

1061: Call to a possibly undefined method addEventListener through a reference with static type Class.

On my stage i have a Button with the instance name stopBtn, this is also exported to an actionscript class called classes.stopBtn (stopBtn.as in a folder called 'classes'). The button is on the first keyframe in the main timeline, in a layer with other buttons on that layer

The error is on line 10 of the stopBtn.as file:

package  classes {

    import flash.display.SimpleButton;
    import flash.events.MouseEvent;
    import flash.display.MovieClip;

    public class stopBtn extends SimpleButton {

        public function stopBtn() {
            stopBtn.addEventListener(MouseEvent.CLICK, stopButtonFunction); 
        }

        function stopButtonFunction(event:MouseEvent):void  {           
                MovieClip(root).trackPosition = 0;
                MovieClip(root).mySoundChannel.stop();
                MovieClip(root).playPause.gotoAndPlay(2);
        }   

    }

}

I have found many threads for the error code 1061 but non of them seem to relate to my problem!

I have tried to ensure all event types are imported by using

Import flash.events.*; but this makes no difference.

Upvotes: 1

Views: 11125

Answers (2)

pho
pho

Reputation: 25489

This one should be pretty obvious. You are trying to add an event listener to the class, not the object.

 stopBtn.addEventListener(MouseEvent.CLICK, stopButtonFunction);

stopBtn is the name of the class you have created. To add the event listener to the instance, modify your code to

this.addEventListener(MouseEvent.CLICK, stopButtonFunction);

That will ensure that you add the event listener to the button object, and not the class like you were trying to (It isn't allowed, as you saw, because it makes no sense)

Upvotes: 3

zaynyatyi
zaynyatyi

Reputation: 1116

You code should be like

package  classes {

    import flash.display.SimpleButton;
    import flash.events.MouseEvent;
    import flash.display.MovieClip;

    public class stopBtn extends SimpleButton {

        public function stopBtn() {
            addEventListener(MouseEvent.CLICK, stopButtonFunction); 
        }

        function stopButtonFunction(event:MouseEvent):void  {           
                MovieClip(root).trackPosition = 0;
                MovieClip(root).mySoundChannel.stop();
                MovieClip(root).playPause.gotoAndPlay(2);
        }   

    }

}

Upvotes: 1

Related Questions