kirupa | 28 May 2007
we started explaining what the code does. While doing
that, you learned about how to add event handlers. In
this page, we'll finish our code explanation and wrap up
- // Getting the clicked
- // Incrementing the scale
- // Fading circle out after
it reaches a certain size
- // Stopping enter
frame event after circle becomes (almost)
The ZoomCircle function fits the standard formula for
being a function called by an event handler. It takes in one
argument of type Event, and its return type is
rest of the code deals with causing our circle to zoom in,
so there is nothing complicated that goes on.
What is worth nothing is stopping the animation by
removing the event listener we added earlier:
- // Stopping enter frame event
after circle becomes (almost) invisible
To remove an event listener, you can use the removeEventListener
function. The arguments you pass to removeEventListener
match the arguments you made earlier when adding the
listener to you object. In other words, both the event and
function called must be the same.
Knowing which object was the source of an event is very
important. In our case, we need to know which movie clip
held the event listener that fired the ZoomCircle function
so that we can remove that event listener.
Determining the object that triggered the event can be
determined by your event object's target method:
In the above line of code from the ZoomCircle function, I
determine which movie clip called the event by checking our
e.target returns an
object of type Object. Since
I am interested in the movie clip version, I can cast the
returned object into a movie clip by typing
object is the what you wish
to typecast. In our case, the object returned by
e.target is typecast into a
This tutorial covered a lot of ground! Hopefully you learned
how to use event handlers to animate dynamic movie clips.
Beyond that, knowing the details such as how to add/remove
event handlers, determining the target of an event, etc. can
come in quite handy.
The ENTER_FRAME event is
what allows you to create smooth animations. Unlike a
traditional loop, an enter frame action does not flood the
internal message queuing mechanism. This means that your
animation can do other things such as respond to mouse
clicks, whereas a loop will freeze your application until it
has run its course.
You have to use addEventListener
to attach an object to an ENTER_FRAME event that will call a
function. This step requires a slight reprogramming of how
you may have learned to dynamically animate (see
older tutorial) in ActionScript 2, but in the long run, you will find the
new way to be more consistent with the syntax and style
found in other programming languages.
If you have a question about this or any other topic, the easiest thing is to drop by our forums where a bunch of the friendliest people you'll ever run into will be happy to help you out!
THE KIRUPA NEWSLETTER
Get cool tips, tricks, selfies, and more...personally hand-delivered to your inbox!
( View past issues for an idea of what you've been missing out on all this time! )