Skip to content

Instantly share code, notes, and snippets.

@sciolist
Created April 17, 2010 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sciolist/369132 to your computer and use it in GitHub Desktop.
Save sciolist/369132 to your computer and use it in GitHub Desktop.
import flash.events.TimerEvent;
class TimerInfo
{
public function new(toInvoke, block)
{
invoke = toInvoke;
blocking = block;
}
public var invoke:Dynamic;
public var blocking:Bool;
}
class Timeline
{
public function new() { _timers = []; }
private var _timers:Array<Dynamic>;
private var _running:Bool;
public function start()
{
if(!_running) next();
}
private function next()
{
_running = false;
if(_timers.length == 0) return;
_running = true;
while(_timers.length > 0)
{
var found = _timers.shift();
if(found == null) continue;
found.invoke();
if(found.blocking) break;
}
}
public function invoke(cb:Dynamic=null)
{
_timers.push(new TimerInfo(cb, false));
return this;
}
public function wait(ms:Float, cb:Dynamic=null)
{
var me = this;
var timer = new flash.utils.Timer(ms, 1);
timer.addEventListener(TimerEvent.TIMER, function(args) { if(cb!=null) cb(); me.next(); });
_timers.push(new TimerInfo(timer.start, true));
return this;
}
private function _tween(start:Float, end:Float, duration:Float, tick:Dynamic, block:Bool=false)
{
var me = this;
var resolution = 16;
var dir = end > start ? 1 : -1;
var total = Math.abs(end-start);
var tickSize = ((total / duration) * resolution) * dir;
var current = start;
var timer = new flash.utils.Timer(resolution, Std.int(duration/resolution));
timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(args) { tick(end); if(block) me.next(); });
timer.addEventListener(TimerEvent.TIMER, function(args)
{
current += tickSize;
tick(current);
});
_timers.push(new TimerInfo(timer.start, block));
return this;
}
public function tween(start:Float, end:Float, duration:Float, tick:Dynamic)
{
return _tween(start, end, duration, tick, true);
}
public function tweenNoBlock(start:Float, end:Float, duration:Float, tick:Dynamic)
{
return _tween(start, end, duration, tick, false);
}
}
var t = 50;
var tl = new Timeline();
tl.tweenNoBlock(1, 0.1, t, function(c) { mc.scaleX = c; });
tl.tween(1, 1.2, t/2, function(c) { mc.scaleY = c; });
tl.invoke(function() { changeImage(); });
tl.tweenNoBlock(0.1, 1, t, function(c) { mc.scaleX = c; });
tl.tween(1.2, 1, t/2, function(c) { mc.scaleY = c; });
tl.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment