Advertisement |
Principles of event driven programming
Events are detected by some object
Usually, events are broadcasted by an instance of an interactive object, typically a graphic on the screen that is a symbol instance. User interactions are, technically speaking, events generated by Flash objects. You then have to write code that can deal with these events. Firstly you must give a name to each symbol instance, users interact with. Otherwise your AS code can't find them.
- So before you code anything in ActionScript that deals with events generated by some user interaction with an object, click on this instance, open the parameters window and fill in label parameter.
This name must be legal:
- Start the label name with a letter
- Do not use whitespaces or punctuation characters or dashes
2.1.2 ActionScript for Flash designers
- All ActionScript goes to the timeline
- Always put AS code into a separate layer, e.g. call it "Action"
- Note: AS2 also would allow you to attach code to instances. You can't do this.
- Action script code extends to frames in the same way as drawings
E.g. if you want the user to interact with buttons after the animation loads:- Click on frame 1 of the "Action" layer
- Hit F9, then code :)
Code will only work within the frames the layer extends to. So if your code is supposed to be valid throughout the animation.- Go to the last frame in your timeline
- Hit F5
2.1.3 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);
Programmers (only): You should be aware that a a component's events inherit from the parent classes. You also can remove a listener with the removeEventListener(). Also the correct explanation is "Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event".
0 comments: