Using Callback Commands instead of Events

Recently I’ve read posts about the speed advantages of callbacks over events on the blogs of Troy Gilbert and Jackson Dustan. But instead of using a strategy like stated in the comment here we use something which we call CallbackCommand and relies to the command pattern. It’s some kind of proxy command to call methods not being part of the command itself.

If you adapt the source of the above mentioned comment and transform it in something like this you don’t have to give a link of the instance holding the method to be called into another. (I’m afraid this will lead to carbage collection issues?)

class MyAPILoader
{
private var _loadCommand: ICommand;
private var _dispObj: DisplayObject;
public function MyAPILoader(url:String, loadCommand:ICommand)
{
_loadCommand = loadCommand;
// initiate the load...
}
private function onLoaded(dispObj:DisplayObject): void
{
_dispObj = dispObj;
_loadCommand.execute();
}

public function get displayObject(): DisplayObject
{
return _dispObj;
}
}

The code of the CommandCallback looks like this:

package rg.command
{
import rg.interfaces.ICommand;

public class CallbackCommand implements ICommand {

private var _func:Function;
private var _args:Array;

public function CallbackCommand(func:Function,...args) {
_func = func;
_args = args;
}

override public function execute() : void {
_func.apply(this,_args);
}
}
}

Finally you settle everything like that:

var loader: MyAPILoader = new MyApiLoader("http://...", new CallbackCommand(loaded, additionalParams));
private function loaded(additionalParams: Type): void
{
addChild(loader.displayObject);
}

Instead of calling simple methods by Callback Commands you get the option to use “real” commands instead additionally. I haven’t checked the performance losses against direct callbacks yet but it shouldn’t be that much slower.

Posted in AS3, Commands, Events, Optimization | 6 Comments

AS3: TimelineEvent, LabelEvent, FrameEvent

I know it’s an old subject but I didn’t use the combination of AS3 and timeline animations for my projects so far.

Coming from Big Spaceship, Adobe DevNet / TimelineWatcher and Bytearray.org I wanted a clean solution to recognize FrameLabels on a timeline without using EnterFrame (which fails if you want to address newly instantiated timeline objects within the given frame) and without extending MovieClip. Mixing all together I come up with this:

FrameEvent.as
TimelineEvents.as

TimelineEvents injects dispatchEvent calls with FrameEvent (carrying currentFrame and currentLabel) using addFrameScript to the timeline on every FrameLabel.

Optional you can inject extra events for BEGIN and END on the timeline by setting addBeginEndEvents to true. By default events get dispatched every time you come across the FrameLabel. You can force it to dispatch only once by setting dispatchOnce value to true. (Be careful! Existing source code within the timeline gets overwritten by using addFrameScript for the corresponding frame. So if you got a stop(); at the end of the timeline and you set addBeginEndEvents to true it won’t stop anymore unless you do a stop within the FrameEvent.END event)

Howto use:

// clip is a MovieClip containing a few framelabels
(new TimelineEvents(clip, true, true));

clip.addEventListener(FrameEvent.LABEL, onFrameLabel);
clip.addEventListener(FrameEvent.BEGIN, onFrameLabel);
clip.addEventListener(FrameEvent.END, onFrameLabel);

private function onFrameLabel(event: FrameEvent): void
{
    switch(event.type)
    {
        case FrameEvent.LABEL:
            trace(event.currentFrame + ": " + event.currentLabel);
            break;
        case FrameEvent.BEGIN:
            trace("you played the first frame");
            break;
        case FrameEvent.END:
            trace("you played the last frame");
            break;
    }
}
Posted in AS3, Development, Events, Flash, Timeline | 2 Comments

Welcome

Hello Blog!

rg.addChild(new Blog());
Posted in Common | Tagged | Leave a comment