[AS3 Chapter 6] Event Handlers
Areost :: Special & Secret :: Schooool
Page 1 of 1
[AS3 Chapter 6] Event Handlers
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:
Now applying our knowledge of functions, we can create a function that responds to this event handler.
...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.
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:
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.
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:
- Code:
object.addEventListener(eventType.Eventname, name);
Now applying our knowledge of functions, we can create a function that responds to this event handler.
- Code:
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.
- Code:
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:
- Code:
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.
Similar topics
» [AS3 Chapter 2] Functions
» [AS3 Chapter 5] Loops
» [AS3 Chapter 1] Variables
» [AS3 Chapter 3] Array Basics
» [AS3 Chapter 4] Conditional Statements and Operators
» [AS3 Chapter 5] Loops
» [AS3 Chapter 1] Variables
» [AS3 Chapter 3] Array Basics
» [AS3 Chapter 4] Conditional Statements and Operators
Areost :: Special & Secret :: Schooool
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Nov 29, 2011 10:27 am by Karma
» Hi guys! Lalala
Mon Nov 28, 2011 9:26 pm by Pukito
» ARGHHHrrhhghghg
Sun Nov 27, 2011 1:25 pm by Pukito
» [Flash Game] - I don't even
Sat Nov 26, 2011 11:08 pm by Karma
» Hi, nice to meet you.
Sat Nov 26, 2011 12:48 am by Pukito
» I think I see ..
Sat Nov 26, 2011 12:47 am by Pukito
» Where Karma goes...
Fri Nov 25, 2011 2:14 am by Karma
» Guys
Tue Nov 22, 2011 9:11 pm by Pukito
» Karmini's Pose
Tue Nov 22, 2011 8:31 pm by Karma
» The Order (1)
Sat Nov 19, 2011 10:42 pm by Karma