Tuesday, September 4, 2018

actionscript3 addEventlistener

ad300
Advertisement
An ActionScript Event is any interaction happening in the Flash environment, whether it was a mouse click, the movement of the timeline to a new frame, the completion of the loading process of an external asset, or any other occurrence. ActionScript can make any object react to any event by using an Event Listener. An event listener is basically a function created with the very specific purpose of reacting to an event. An object can react to an event using an event listener. This is done by using the .addEventListenermethod. This method simply registers an Event Listener and an Event to an object.
The process described above is written in ActionScript using in the format shown below:
myObject.addEventListener(Event, eventListenerFunction);
Our Event Listener will obviously have to be specified by declaring the function the same way any other function is declared in ActionScript, the only difference is that the listener function must have one argument to represent the event. This event argument can have any identifier as its name, I usually use the letter 'e' for it as shown in the generalized code below:
myObject.addEventListener(Event, eventListenerFunction);

function eventListenerFunction (e:Event):void{
//ActionScript statements to be executed when the event happens.
}
So for example, if we want a graphical object placed on stage to act like a button by reacting to a mouse click over it, we can simply register an event and an event listener to it this way:
myButton.addEventListener(MouseEvent.CLICK, myClickReaction);

function myClickReaction (e:MouseEvent):void{
trace("I was clicked!");
}
The format above is the basic format at which any event can be registered. Depending on the event you are trying to listen to, the object and the event to register for will differ. For example, if you are using the Loader Class to load an external asset at run time, you can perform a specific action only when the asset you are trying to load finishes loading. For this particular purpose you will need to register for the Event.COMPLETE as shown in the example below:
my_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, startListener);

function startListener (e:Event):void{
trace("Loading Completed");
}
You need to study each class on its own to learn about its supported events and how to use those specific events related to it. The easiest way for doing this is by checking the built-in ActionScript Reference.
Share This
Previous Post
Next Post

Pellentesque vitae lectus in mauris sollicitudin ornare sit amet eget ligula. Donec pharetra, arcu eu consectetur semper, est nulla sodales risus, vel efficitur orci justo quis tellus. Phasellus sit amet est pharetra

0 comments:

Ad Section2

Sponsor

Ad Section