Please note that ExpatTech is close for the Christmas and New Year holidays. We will re-open on 2nd January 2024.

ExpatTech Techblog

Peter Todd 2008.10.31. 15:39

Flash Cookbook - Events - custom events in Flash AS3

Recipe for releasing an event in Flash Actionscript 3.0

Releasing our own events is quite useful in applications. We can track everything what is going on in our app and makes it possible to make object-communication easy, nice and fast.

Ingredients:

- an object (1 pc)

- some events and handlers (1 pc)

Steps:

- create an object

- add a function (1) (this will fire an event)

- add this object to the timeline

- call function (1) to test the event.

Process of an event:

something happened -> EVENT -> calling a function

When an event is released usually a function is called and you can make some further actions. If you need some data from the object then it's quite practical to add a property to it. It has to be public as we are going to use it from outside the object when the event is fired.

function (1):

function spearFisher(event:Event):void
{
trace(event.target.data);
}

event.target is the object what is firing the event, in this example it has a property, called "data"

adding listener:

OBJECT'S_NAME.addEventListener(NAME_OF_EVENT, FUCTION_TO_CALL)

==============================

Source of the object (path = classes/Tarzan.as):

package classes
{
import flash.display.Sprite;
import flash.events.Event;

public class Tarzan extends Sprite
{
public var data:String; //property to hold data

public function Tarzan() //constructor
{
}

public function whoAreYou():void //function with event
{
data = "me tarzan"; //setting data
dispatchEvent(new Event("communication_skill_is_shown")); //EVENT
}
}
}

==============================

We can test it now:

import classes.Tarzan;

var tarzan:Tarzan = new Tarzan();
addChild(tarzan);
tarzan.addEventListener("communication_skill_is_shown", spearFisher); //adding listener
tarzan.whoAreYou()

function spearFisher(event:Event):void
{
trace(event.target.data);
}

==============================

Output:

me tarzan

==============================

Other solution:

Even though events are brilliant you can still solve the problem without event listeners by calling the parent's function directly. It's something like this:

something happened in the object -> calling parent's function

This solution also works fine but makes our class weak because we have to write the parent's function's name into the class and this destroys the dynamic reuse of the class. If you use an event instead then it doesn't matter what type the parent is, what is the name of the function (which is called at the event) and doesn't even matter if you listen to the event or not.

==============================

Data i/o and multiuser applications are unimaginable without events. It can make a hundred times easier to write a chat app with events rather than the solution mentioned above. The code will be clean, nice and easy to understand or modify.