What are Event Listeners?
Event Listeners allow objects to become active and listen for specific instructions. For example say you had ten movie clips on the stage but you only wanted one of those movie clips to respond to a mouse click. You would use an event listener to achieve this goal.
To write an event listener is pretty easy;
You start with your movie clip instance name or the name of your object and add the addEventListener() method. Inside the brackets of the addEventListener method is where we declare what type of event you want to listen for. In this case we are listening for a mouse click. We do this by first accessing our MouseEvent class and selecting CLICK from that class. Finally we give our addEventListener method a unique name of our own choice; in the above case we've used mouseClick.
Our event listener is now ready and waiting for a mouse click. To make it do something when the mouse has been clicked you would write a function for the event listener. Visit our Mouse Event chapter to find out how to do this.
Removing Event Listeners
Every event listener uses up memory so it's a good idea to remove the event listeners that are no longer needed in the application. So if you wanted to remove the above event listener you'd simply write.
Simple.
You can see event listeners in action throughout this AS3 Guide check the following chapters;
This post is tagged AS3 Guide, Event Listeners
Share This Post
Jobs


























5 Comments
Thanks for this instruction on event listeners!
However, when I tried to use it for a button to navigate to another frame in the timeline, I got the dreaded TypeError: Error #1009. Here's the error:
TypeError: Error #1009: Cannot access a property or method of a null object reference. at mywebsite_fla::MainTimeline/clickHandler1()
Could anyone please tell me what I did wrong in coding the add/remove of the event listener?
Here's my actionscript 3 code for the button logo_btn to navigate to frame " secret" . The logo_btn is NOT on frame "secret" and eventhough I've set the removeEventListener, the error says it still seeks for the button.
logo_btn.addEventListener(MouseEvent.CLICK, clickHandler1);
function clickHandler1(event:MouseEvent):void {
if(event.type == "click"){
gotoAndStop("secret");
logo_btn.removeEventListener(MouseEvent.CLICK, clickHandler1);
}
}
Thanks in advance for helping me!
Regards,
Michiel
Michiel,
Try this:
function clickHandler1(event:MouseEvent):void
{
// you don't need the if (event.type == "click"), since you registered a MouseEvent.CLICK
var localMC:MovieClip = e.currentTarget as MovieClip;
localMC.removeEventListener(MouseEvent.CLICK, clickHandler1);
gotoAndStop("secret");
}
Now, I don't know where in your program this code is. Is it on the root timeline? That 'gotoAndStop("secret");' is referring to the main timeline?
I have a main time line with 6 frames each with a stop action. I have a navigation system with 5 buttons in a mc in a mc. I try assigning a
mybutton.addEventListener(MouseEvent.CLICK, MouseClick);
function MouseClick(Event:MouseEvent):void{
gotoAndPlay(4);
}
how do you make it so that the button click of the mybutton will send the head to go to the main time line frame 4. Thanks. I am hoping to have all the action to be in the same frame.
to yohei:
just change gotoAndPlay(4) to mybutton.gotoAndPlay(4);
stop();
btn1.addEventListener(MouseEvent.ROLL_OVER,playover1);
btn2.addEventListener(MouseEvent.ROLL_OVER,playover2);
btn3.addEventListener(MouseEvent.ROLL_OVER,playover3);
btn4.addEventListener(MouseEvent.ROLL_OVER,playover4);
btn5.addEventListener(MouseEvent.ROLL_OVER,playover5);
btn6.addEventListener(MouseEvent.ROLL_OVER,playover6);
btn1.addEventListener(MouseEvent.ROLL_OUT,playout1);
btn2.addEventListener(MouseEvent.ROLL_OUT,playout2);
btn3.addEventListener(MouseEvent.ROLL_OUT,playout3);
btn4.addEventListener(MouseEvent.ROLL_OUT,playout4);
btn5.addEventListener(MouseEvent.ROLL_OUT,playout5);
btn6.addEventListener(MouseEvent.ROLL_OUT,playout6);
btn1.addEventListener(MouseEvent.MOUSE_DOWN, btnpress);
function playover1(event:MouseEvent):void{
btn1.gotoAndPlay(2);
}
function playover2(event:MouseEvent):void{
btn2.gotoAndPlay(2);
}
function playover3(event:MouseEvent):void{
btn3.gotoAndPlay(2);
}
function playover4(event:MouseEvent):void{
btn4.gotoAndPlay(2);
}
function playover5(event:MouseEvent):void{
btn5.gotoAndPlay(2);
}
function playover6(event:MouseEvent):void{
btn6.gotoAndPlay(2);
}
function playout1(event:MouseEvent):void{
btn1.gotoAndPlay(16);
}
function playout2(event:MouseEvent):void{
btn2.gotoAndPlay(16);
}
function playout3(event:MouseEvent):void{
btn3.gotoAndPlay(16);
}
function playout4(event:MouseEvent):void{
btn4.gotoAndPlay(16);
}
function playout5(event:MouseEvent):void{
btn5.gotoAndPlay(16);
}
function playout6(event:MouseEvent):void{
btn6.gotoAndPlay(16);
}
function btnpress(event:MouseEvent):void{
btn1.gotoAndPlay(14);
stop();
}
how to stop the animation if my button is already press and anyone can make my code shorter. i am using for loops but it doesnt work at all.
Incoming Links
Leave a Reply