开发者

AS3 calling functions in functions and avoiding the player to crash / hang?

I have a series of calculations i'm doing over a bunch of objects stored in a array. Each function is pretty CPU demanding but if you only run one function, it just works fine. Here a开发者_开发问答 shema :

var array:Array = new Array();

function a():void{
  //Do some stuff with array
b();
}

function b():void{
  //Do some stuff with array
c();
}

function c():void{
  //Do some stuff with array
d()...
}

Back in AS2.0, i found that if i ran a very short "Tween" between the call of functions (like 200 ms), i could make the player not crash/hang

var t:Tween = new Tween(...
t.onMotionFinished = function(){
b();
}

I'm looking for a more "conventional" way :)


You can use Timer

var timer:Timer = new Timer(200,0);
timer.addEventListener(TimerEvent.TIMER,timerHandler);

...

protected function timerHandler(e:Event):void {
    b();
}


You can use Timmer, or you can do something like this :

var oldTime:Number = getTimer();
var thisTime:Number = 0;

var counter:int = 0;
var functions:Vector<Function> = new Vector<Function>();
functions.push(a,b,c);

addEventListener(Event.ENTER_FRAME, onLoop);

private function onLoop(e:Event):void
{
   var resultTime:Number = getTimer() - oldTime;
   if(resultTime > 200 )
   {
      functions[counter].call();
      ++counter;
      oldTime += resultTime;
      if(counter >= functions.length) 
      {
        counter = 0;
        removeEventListener(Event.ENTER_FRAME,onLoop);
      } 
   }
}

Just add ENTER_FRAME listener when the execution needs to start. I didn't check the code in details, but I hope will work for you ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜