I used to hate writing event listeners and handlers in Flash
and I would always put my code on movie clips. Well now that you can’t put code on movie clips anymore with
ActionScript3, I may have to start using event listeners and handlers. I opened the Lynda.com video on event listeners
and handlers and found that writing them in ActionScript 3 is a lot easier than
it was in ActionScript 2.
I proceeded with the tutorial by adding an event listener to
the snow boarder instance name. I
was excited that listeners could be created with the instance name and not
having to create a new variable and then going though extra steps to include
the instance name. The listener
was composed of an event, which in this case was a mouse event – “CLICK”. After the event, I typed the name of
the function.
The next video discussed about writing the function or
handler for the event listener. I
have written functions before, but have never included events, so this was a
fun learning experience. The
function listed the mouse event from above as its variable and I followed with
the tutorial by creating a trace statement to see if the function worked
properly. Everything here was
pretty simple until I found out that the instance name in the function could be
replaced by an event as well. This
would make the function reusable for any object! Usually, I would have had to create a variable to use for
the instance name, but by using event.target, I was able to make this function
dynamic. So instead of controlling
the x and y coordinates or any other property of only 1 object, in this case
boarder_mc, I used event.target, which in turn will work for any object inside
that function.
Another fun thing I learned tonight is making the mouse
cursor available in ActionScript 3.
In ActionScript 2 the code was – objectName.useHandCursor = true/false
(true makes it visible and false makes it invisible).
In ActionScript 3, the mouse cursor seems to be off by
default when using events, but the new code is now – objectName.buttonMode =
true/false
Below is the result of the mouse event. Every time you click on the snow
boarder object, it will move up 15 pixels from its current location and will
increment the rotation by 45 from its current rotation.
0 comments:
Post a Comment