In continuation with the tutorial (001 - 002 - 003 - 004)
3.2.1. Using eventListeners?
The concept of eventListeners is quite simple, to the simple statement in the code that instantiates should listen particular event and "warn" when it happens so we can execute certain function.
There is a detail that we have to take into account, certain events are not supported by certain instances, for example, an event keyPress will never be fired in an image. What I mean is that as I said earlier, this also depends on the events of the bodies. For example, we want the user to click with the mouse on a button to run a function (funcao_a_executar), which must be called without the () (this is of the few cases where the function can (must) be called without the ()).
MeuBotão1.addEventListener (MouseEvent.CLICK, funcao_a_executar); :MouseEvent): void { funcao_a_executar private function (event: MouseEvent): void (
Trace ( "button click") ) We have noted that 1 point very important, as funcao_a_executar () is fired from a MouseEvent, she must receive parameters and characteristics of this event (some information about the instance) just so you have to switch to receive the "father" of this event In this case the "father" is the MouseEvent. An argument very useful step in that event is the target or currentTarget indicates that in the instance that fired the event to which we can access even without knowing the name of that instance.
A simple example:
We have 2 buttons to increase / decrease the size of an image 5 pixels, we would do-well:
botao_aumenta.addEventListener (MouseEvent.CLICK, increases); botao_diminui.addEventListener (MouseEvent.CLICK, decreases); { private function increases (evt: MouseEvent): void (
/ / to know the name of the instance: Trace ( "instance:" + evt.target); Imagem1.width + = 5; ) { private function decreases (evt: MouseEvent): void (
/ / to know the name of the instance: Trace ( "instance:" + evt.target); Imagem1.width -= 5; ) The symbol + = -= or serves to add or reduce the values of some variable / property.
We can assign several eventListners the same subject:
meuBotão1.addEventListener (MouseEvent.CLICK, botao_click); meuBotão1.addEventListener (MouseEvent.MOUSE_OVER, botao_rato_em_cima); meuBotão1.addEventListener (MouseEvent.MOUSE_OUT, botao_rato_fora); and all functions receive the same kind of event:
{} botao_click private function (evt: MouseEvent): void ()
{} botao_rato_em_cima private function (evt: MouseEvent): void ()
{} botao_rato_fora private function (evt: MouseEvent): void ()
Finally, we can avoid the use of 3 functions, and reuse the code using the parameters passed by the event, to know what kind of event was fired (if all are "children" of the same event (MouseEvent)):
meuBotão1.addEventListener (MouseEvent.CLICK, botao_evento); meuBotão1.addEventListener (MouseEvent.MOUSE_OVER, botao_evento); meuBotão1.addEventListener (MouseEvent.MOUSE_OUT, botao_evento); and function:
{ botao_evento private function (evt: MouseEvent): void (
/ * using a switch to find out what kind of event was fired, accessing the Parameter type of evt * / switch (evt.type) ( MouseEvent.CLICK case: Trace ( "Click event"); break; MouseEvent.MOUSE_OVER case: Trace ( "Event MOUSE OVER"); break; MouseEvent.MOUSE_OUT case: Trace ( "Event MOUSE_OUT"); break; ) ) As you can see, we can save a few lines of code and optimize the outcome of the swf using the function botao_evento to deal with any type of mouse events (MouseEvent).
We can also use events to call methods of other objects, always using a function, using the function on top:
MouseEvent.CLICK case: meuBotao1.move (btn.x 5, btn.y +5); break; In this case we call the method move () of the button, that is to say the button that should move 5px to the right and 5px down. This will be run each time the user clicks the button.
Finally, we can still do oo button to shoot another event, now entering domestic properties in the button and forcing him to shoot an internal event, using the function on top, I want you to move your mouse over the button, which is triggered the event click the button:
MouseEvent.MOUSE_OVER case: meuBotao1.dispatchEvent (new MouseEvent (MouseEvent.CLICK)); break; ( MouseEvent.click )); Se mantivermos toda a nossa função em cima com o switch (retirando o MOUSE_OUT): with the line up I say to my instance / button (meuBotao1) to fire the Click event, for that we must force the shoot to a new event, exactly equal to what is waiting eventListener (meuBotão1.addEventListener (MouseEvent . CLICK)) also indicates the type of event (MouseEvent), or otherwise He shot the event, but nothing happens; meuBotao1.dispatchEvent (new MouseEvent (MouseEvent.CLICK)); If we keep all our light up with the switch (removing the MOUSE_OUT):
{ botao_evento private function (evt: MouseEvent): void (
switch (evt.type) ( MouseEvent.CLICK case: meuBotao1.move (btn.x 5, btn.y +5); break; MouseEvent.MOUSE_OVER case: meuBotao1.dispatchEvent (new MouseEvent (MouseEvent.CLICK)); break; ) ) If that happens you pass the mouse over or click the meuBotao1 be interpreted as if it clicked, as will be triggered in the event CLICK same in both cases, which will cause meuBotao1 call the method. Move ();
This can easily understand with the following procedure:
User passes on mouse meuBotão1 over -> Shoot the event MOUSE_OVER -> The eventListner draws the line botao_evento () -> this function detects the event as MOUSE_OVER -> Require the button to shoot the event CLICK -> the eventListner draws the line botao_evento () again -> this function detects the event as CLICK -> run method. move the button
Well, for now is all ...
Hug.
http://creativecommons.org/licenses/by-nc-nd/3.0/deed.pt














4 Comments
Congratulations, great tutorial!
This is not correct: addEventListner.
More yes: addEventListener.
Good sam, it is true ... how much of the code was written without the validation, I ended up committing this error over the entire tutorial ... was to be copied from one place to another ...
corrected!
Thanks and Regards.
mario, how do you link a link or button linkbar?
already tried by way of actionscript, and mxml, but n t giving.
can you help me?
thanks.