Event management is a very powerful tool you can utilise when dealing with multiple swf files and/or classes. It’s a great way of communicating between different elements in your project and is not difficult to use.
Let’s begin by showing you the basic code for you to copy and paste, quick and simple. I’ll then explain in more detail.
First of all, you can add dispatchEvent(new Event(“myEvent”)); to any function in your code. This sends out a signal called “myEvent” using the built in function ‘dispatchEvent’.
You can then setup an event listener to wait for the signal to be dispatched, and then act upon it: addEventListener(“myEvent”, doMyEvent);
where ‘doMyEvent’ is another function which gets executed when the signal is dispatched.
Of course, you need to import flash.events.Event into the class to allow events to be dispatched. So here’s a full example, with the event dispatched on a mouse click:
package
{
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
/**
* @author Andy Jones, Arcimedia
*/
public class EventExample extends Sprite
{
public function EventExample()
{
btn.addEventListener(MouseEvent.CLICK, mouseClick);
addEventListener("myEvent", doMyEvent);
}
private function mouseClick(evt:MouseEvent)
{
dispatchEvent(new Event("myEvent"));
}
private function doMyEvent(evt:Event)
{
trace("event received");
// Do some stuff
}
}
}
This may appear unnecessary at first – why use dispatchEvent on a mouse click when you can just put the code in the mouse click function?
Very true, but if you wanted to execute some code located in another class or another swf file, then suddenly dispatchEvent becomes very useful.
If you had two classes, one loading the other, then the child may dispatch an event once it has finished completing a certain task. The parent class can then act upon this event. For example, the child class might load a gallery of images; when all the images have loaded, it tells the parent class using dispatchEvent, which then displays a control panel to scroll through the images.
Here’s the code for both parent and child classes:
package
{
import flash.events.Event;
import flash.display.Sprite;
/**
* @author Andy Jones, Arcimedia
*/
public class Parent extends Sprite
{
private var child:Child;
public function Parent()
{
child = new Child();
child.addEventListener("imagesLoaded", doMyEvent);
addChild(child);
child.init();
}
private function doMyEvent(evt:Event)
{
trace("event received");
// Load controls
}
}
}
package
{
import flash.events.Event;
import flash.display.Sprite;
/**
* @author Andy Jones, Arcimedia
*/
public class Child extends Sprite
{
public function Child()
{
}
public function init():void
{
//code to load images, when all images have been loaded,
imagesLoaded(); //is called
}
private function imagesLoaded():void
{
dispatchEvent(new Event("imagesLoaded"));
}
}
}
For more advanced situations, where you have lots of events firing off in different places, I would advise creating an Event class which lists all your events in one place and eases development changes. You can also pass parameters to the parent class, for example, here I have passed a string parameter. This can then be accessed in the parent file in the ‘doMyEvent’ function. Here is the code for the Event class:
package
{
import flash.events.Event;
/**
* @author Andy Jones, Arcimedia
*/
public class AllEvents extends Event
{
public static const IMAGES_LOADED:String = "imagesLoaded";
public static const IMAGE_CLICKED:String = "imagesClicked";
public static const NEXT_CLICKED:String = "nextClicked";
public static const PREVIOUS_CLICKED:String = "previousClicked";
public var _data:String;
public function AllEvents(type:String, Data:String, bubbles:Boolean = false, cancelable:Boolean = false)
{
super(type, bubbles, cancelable);
_data = Data;
}
}
}
The parent and child classes would then become:
package
{
import flash.events.Event;
import flash.display.Sprite;
import AllEvents;
public class Parent extends Sprite
{
private var child:Child;
public function Parent()
{
child = new Child();
child.addEventListener(AllEvents.IMAGES_LOADED, doMyEvent);
addChild(child);
child.init();
}
private function doMyEvent(evt:AllEvents)
{
trace("event received = "+evt._data);
// Load controls
}
}
}
package
{
import flash.events.Event;
import flash.display.Sprite;
import AllEvents;
public class Child extends Sprite
{
public function Child()
{
}
public function init():void
{
//code to load images, when all images have been loaded,
imagesLoaded(); //is called
}
private function imagesLoaded():void
{
dispatchEvent(new AllEvents(AllEvents.IMAGES_LOADED, "Image Gallery"));
}
}
}

Designing in a minimalist style can yield a beautiful website, where clarity and simplicity in the structure of the site is reflected in the formal design of it. Recently, I have been re-reading the fundamentals of minimalism as a coherent artistic ideal, which can be traced back to various American artists of the 1960s, such as Carl Andre, Frank Stella and Donald Judd. The small but elemental list of minimalist ‘laws’ that determined the shape of minimalist art and design in the decades that followed is worth recollecting:











