Menu

#6 Improved doLater

open
nobody
None
5
2005-12-22
2005-12-22
No

http://play.ground.gr/?p=82

There is often a need to postpone execution of the
function. doLater method of UIObject is doing the job,
but sometimes we need to be able to pass parameters and
to delay execution more than one frame. Here is the
code that I am using:

private var laterQueue:Array;
/*
/ o - object to which function is applied
/ f - name of the function
/ p - array of parameters
/ d - number of frames to delay
*/
function doLater(o:Object, f:String, p:Array,
d:Number):Void
{
var a = [ o, f, p ];
for ( var i = 1; i < d; i++ ) a = [ this, "doLater", a ];

if ( !laterQueue ) laterQueue = [];
laterQueue.push( a );
onEnterFrame = doLaterDispatcher;
}
private function doLaterDispatcher(Void):Void
{
delete onEnterFrame;
var __laterQueue:Array = laterQueue;
// empty main array
laterQueue = [];
while ( __laterQueue.length != 0 ) {

var z = __laterQueue.shift();
z[ 0 ][ z[ 1 ] ].apply(z[ 0 ], z[ 2 ]);

}
}

For example, to gotoAndPlay(12) in movieclip instance
named "myMC", after delay of 3 frames:

doLater( myMC, "gotoAndPlay", [12], 3 );

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.