Tags

Activity Industry Sector
Arts, Media & Entertainment

Activity Originally Created By: Mike Morris

Closure

Part of Lesson Plan: Animating Objects Programmatically-Animating an Analog Clock face: Part3

Activity Overview / Details

Closure:

One final thing we need to add at the very top of our code is an event listener.

Every component broadcasts events when a user interacts with it. When a user clicks a Button, for example, it dispatches a MouseEvent.CLICK event and when a user selects an item in a List, the List dispatches an Event.CHANGE event. An event can also occur when something significant happens to a component such as when content finishes loading for a UILoader instance, generating an Event.COMPLETE event. To handle an event, you write ActionScript code that executes when the event occurs.

Event registration

For each event (user action or something else) you would like to intercept, you must register a so-called event listener function.

The syntax is the following:

addEventListener(
Type_of_event.
Name_of_event, Name_of_function_YOU_define);

Example:

  • Let's say you have a button instance. In the parameters panel you named it hello_button.
  • If you want to tell the button to watch out for user clicks then you have to write something like to register the event with a function (see below).
  • So goto the ActionScript layer and hit F9. Then type:

hello_button.addEventListener(MouseEvent.CLICK, click_handler);

What Our Event Listener will look like:

We'll be talking about 'Event Listeners' in a later assignment, but what you need to know for now is that the code (shown below) tells flash to run the 'update' function every time the current frame changes. So if our flash document was set to 15 frames per second, the 'update' function would be called 15 times every second.

 

this.addEventListener(Event.ENTER_FRAME, update);

 

Please make sure the above code is the first line of code within your actionscript panel

Materials / Resource

No resources are included, yet.