Central Event System

The Fleb Framework uses a central event system you can also easily use for your own events. Some coders have the tendency to create public methods inside a class and use these methods when needed (they call these functions inside the normal flow). I strongly suggest not to do so. I prefer using this event system when possible. It helps a lot when you will later try to debug the project, make a small fix or whatever.

Add the event

Open “ProjectEvent.as” and add your event:

public static const SOUND_PLAYING:String = "ProjectEvent.onSoundPlaying";
public static const YOUR_EVENT:String = "ProjectEvent.onYourEvent"; 

Dispatch the event

Open the desired class and dispatch the event in the desired place:

import net.kaegi.utils.VarCentral;
var varcentral:Object = VarCentral.getInstance().vars;

varcentral.flebEvents.dispatchEvent(new ProjectEvent(ProjectEvent.YOUR_EVENT));

You can have any number of parameters (of any type) dispatched with the event:

varcentral.flebEvents.dispatchEvent(new ProjectEvent(ProjectEvent.YOUR_EVENT, {anyParameter:"here is my parameter", test:[1,2,3,4]}));

Add an event listener

Open the desired class and add the event listener:

import net.kaegi.utils.VarCentral;
import ProjectEvent;
var varcentral:Object = VarCentral.getInstance().vars;

private function addListeners():void 
{
    varcentral.flebEvents.addEventListener(ProjectEvent.YOUR_EVENT, onYourEvent);
}
private function onYourEvent(event:ProjectEvent) 
{
    // do something here
}

Having sent some parameters you can use them like this:

private function onYourEvent(event:ProjectEvent) 
{
    trace("ProjectEvent.YOUR_EVENT dispatched! anyParameter: "+event.params.anyParameter+", test: "+event.params.test; 
}

Remove an event

Don't forget to remove events when they are not needed anymore!

private function kill():void 
{
    varcentral.flebEvents.removeEventListener(ProjectEvent.YOUR_EVENT, onYourEvent);
}

Visit the original fleb page.

Post A Comment

Anti-Spam Quiz: