[Golist] Sequencing function calls and passing arguments
Jud Holliday
judh at ZAAZ.com
Tue May 20 22:16:28 PDT 2008
Okay, so I know I'm a bit behind the times here, but this past week I finally got my head around how to sequence with Go and I have to say that so far I am loving it!
We have been using Tweego for some simple Fuse-like sequencing and had also developed a class to tween around an ellipse, but when I found myself wanting to sequence our custom ellipse tween with some other properties it finally dawned on me that SequenceCA is what I needed to use and that I also had to start putting together some basic tween classes for simple display object properties. Moses I know you've dropped some hints about this and have mentioned wanting someone to take this up, but so far I haven't seen anything posted to the playground along these lines.
So, long story short, I have started to put together some basic tweening classes (and Graeme is making new additions as well) which will be posted soon to the playground. Included so far are:
PositionTween, AlphaTween, BlurTween, and CallbackTrigger
The first 3 are probably obvious. CallbackTrigger is actually pretty simple too, but I have a question which I don't think I have seen anybody post about yet.
The basic idea is that you can easily sequence function calls (which I believe someone else on the list mentioned, but I haven't seen an example of yet). So for example:
var seq:SequenceCA = new SequenceCA();
//fade up 'square' to alpha of 1 over .8 secs
seq.addStep( new AlphaTween(square, 1, .8, 0, Quartic.easeOut) );
//at the same time the alpha starts, call the 'close' method on menu1
seq.addStep( new CallbackTrigger(menu1.close), true );
//call the 'open' method on menu2 after a .5 second delay
seq.addStep( new CallbackTrigger(menu2.open, .5), true);
//the sequence will not advance further until it receives the 'OPEN_COMPLETE' event from menu2
seq.lastStep.advance = new OnEventComplete(menu2, Menu.OPEN_COMPLETE);
//fade out square2
seq.addStep( new AlphaTween(square2, 0, .5, Quartic.easeOut) );
seq.start();
This is just a simple example, but hopefully it all makes sense. CallbackTrigger is actually an extremely basic class. It essentially just calls the 'addCallback' method on LinearGo with the 'type' set to GoEvent.START.
So the next thing I wanted to do is pass some arguments to the function call, similar to the way you could back in Fuse: {scope: this, func:"setSelected", args:[arg1, arg2]}. My preferred way to do that would have been to override 'addCallback', but it requires passing another argument to the method, so a straight override won't work. So my solution (at least for now) was to add a new method called 'addCallbackWithParams' which takes the extra arguments parameter. The full code is below. My questions are:
1. Am I going about this in an overly complicated way?/Is there already a system in place to accomplish this?
2. If not, then does addCallbackWithParams make sense, or is there another way to add the ability to pass parameters to a callback in LinearGo? This seems like a really handy feature.
I know I could set up listeners or use class variables and accomplish the task through other means, but it just seems way more convenient to do it all in one simple step.
Code is below. Love to hear whatever feedback people have.
Thanks,
Jud
public class CallbackTrigger extends LinearGo
{
protected var _closure:Number;
protected var _arguments:Dictionary;
/**
* @closure The function that will be called
* @delay The delay in seconds before the callback will be triggered
* @arguments An optional array of arguments that will be passed to the function
*/
public function CallbackTrigger( closure:Function = null, delay:Number = NaN, arguments:Array = null )
{
if (closure != null)
{
_arguments = new Dictionary();
var type:String = GoEvent.START;
addCallbackWithParams(closure, type, arguments);
}
if(!isNaN(delay))
{
super._delay = delay;
}
}
/**
* Adds a callback with parameters.
* Would like to override 'addCallback' but that is not possible with a different method signature.
*
* @param closure A reference to a function
* @param type A GoEvent constant, default is START.
* @param arguments An array of arguments to pass to the function
*/
public function addCallbackWithParams(closure : Function, type:String=GoEvent.START, arguments:Array = null):void {
super.addCallback(closure, type);
//set arguments to call in the closure
if (!_arguments[ type ])
{
_arguments[ type ] = new Array();
}
var a:Array = (_arguments[ type ] as Array);
if (a.indexOf(closure)==-1)
{
a.push(arguments);
}
}
/**
* Overriden so that any arguments passed in can be passed
* in to the callback using 'apply'
*/
override protected function dispatch(type:String):void
{
var funcs:Array = (_callbacks[ type ] as Array);
var args:Array = (_arguments[ type ] as Array);
if (funcs)
{
for (var i:int; i<funcs.length; i++)
{
funcs[i].apply(null, args[i]);
}
}
if (hasEventListener(type))
{
dispatchEvent(new GoEvent( type ));
}
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://goasap.org/pipermail/golist_goasap.org/attachments/20080520/84e47754/attachment-0001.html
More information about the GoList
mailing list