Latest topics
[AS3 Chapter 6] Event Handlers EmptyTue Nov 29, 2011 10:27 am by Karma

» Hi guys! Lalala
[AS3 Chapter 6] Event Handlers EmptyMon Nov 28, 2011 9:26 pm by Pukito

» ARGHHHrrhhghghg
[AS3 Chapter 6] Event Handlers EmptySun Nov 27, 2011 1:25 pm by Pukito

» [Flash Game] - I don't even
[AS3 Chapter 6] Event Handlers EmptySat Nov 26, 2011 11:08 pm by Karma

» Hi, nice to meet you.
[AS3 Chapter 6] Event Handlers EmptySat Nov 26, 2011 12:48 am by Pukito

» I think I see ..
[AS3 Chapter 6] Event Handlers EmptySat Nov 26, 2011 12:47 am by Pukito

» Where Karma goes...
[AS3 Chapter 6] Event Handlers EmptyFri Nov 25, 2011 2:14 am by Karma

» Guys
[AS3 Chapter 6] Event Handlers EmptyTue Nov 22, 2011 9:11 pm by Pukito

» Karmini's Pose
[AS3 Chapter 6] Event Handlers EmptyTue Nov 22, 2011 8:31 pm by Karma

» The Order (1)
[AS3 Chapter 6] Event Handlers EmptySat Nov 19, 2011 10:42 pm by Karma

May 2019

Calendar Calendar

[AS3 Chapter 6] Event Handlers

Go down

[AS3 Chapter 6] Event Handlers Empty [AS3 Chapter 6] Event Handlers

Post by Pukito on Wed Oct 12, 2011 10:58 pm

This will be a very short tutorial, because there is not much information that can be talked about regarding event handlers. However, just because there is not much information doesn't mean that event handlers are not important. In fact, handling events is a major part of interactivity. Nothing would work if the events cannot be handled.

First of all in actionscript 3, we must place an event listener. This does exactly what it sounds like -- it listens (detects) events that happen such as mouse clicks, keyboard clicks etc. The general code for an event handler looks like:

object.addEventListener(eventType.Eventname, name);

Now applying our knowledge of functions, we can create a function that responds to this event handler.


function name (eventObject:eventType):void{
//some code here

...and by putting it all together, we can create a complete event handler. For example, here is an event handler for a mouse click on a movie instance called rainybellyButton.


function sheGiggles(eventObject:EventType):void
    // Actions performed in response to the event go here.
rainybellyButton.addEventListener(MouseEvent.CLICK, sheGiggles);

Removing Event Listeners

Sometimes there are instances where you may need to remove an event listener. This may be because you no longer need the event listener anymore, or if you are moving onto a new frame. In the case that you go to a new frame and there is an event listener on the previous frame, then that event listener will still be active unless you remove it. This will interfere with the program, and could potentially cause many bugs. That is why removing event handlers is necessary.

I remember this one time when I was programming a keyboard-navigational game snippet. I had programmed the segment of the game wonderfully. But then, when I was done with the map on the frame, I moved to the next frame. That's where there were many bugs. I did not know why there were these bugs, and it took me a loooong time to figure out the reasons for these bugs -- I did not remove my event handlers! Ahahaha...

The code needed to remove an event handler is as follows:


movieInstance.removeEventListener(eventType.EVENTNAME, name);

Yes. As you can see, it is the same as adding an event listener, except the "add" is replaced with "remove". That's all!

...and that's it for this chapter (I said that it would be a short one). I am off to do greater things with the little time that I have left. Very Happy

Put on your sunglasses... Cool and put on your 'fro... afro . Because I'm here now clown.

My friends list:

Rainy albino
Karma scratch
Serene pig

Posts : 377
Monies : 3035
Reputation : 4
Join date : 2011-09-27
Location : MHMM.

View user profile

Back to top Go down

Back to top

Permissions in this forum:
You cannot reply to topics in this forum