Question

Let's say I have a series of animations that I want to execute in sequence. If I do it like this, am I creating a huge call stack that eats up more memory than necessary?

function ch1():void {
    var someVar:uint;
    function doThis();
    ...
    ...
    ch2();
}
function ch2():void {
    var someOtherVar:String;
    function doThat();
    ...
    ...
    ch3();
}
function ch3():void {
    var evenMoreVar:Number;
    function doMore();
    ...
    ...
    ch4();
}

Would it be better to call the next function by doing something like this to avoid a huge call stack?

function ch1():void {
    addEventListener("ch1_end",ch2);
    var someVar:uint;
    function doThis();
    ...
    ...
    dispatchEvent(new Event("ch1_end"));
}
Was it helpful?

Solution

Using events is a better way to deal with sequences like yours, but in some simple cases you can just delay each call a little so you can be sure they will not run in the same time and cause freezing your swf , try to use something simple like setTimeout and see the results, if it is not good then you should try useing the events as you have described in your question .

var nextCallDelay:Number = 20; // in milliseconds, change it to fit your needs

function ch1():void {
    var someVar:uint;
    function doThis();
    ...
    ...
    setTimeout(ch2, nextCallDelay);
}
function ch2():void {
    var someOtherVar:String;
    function doThat();
    ...
    ...
    setTimeout(ch3, nextCallDelay);
}
function ch3():void {
    var evenMoreVar:Number;
    function doMore();
    ...
    ...
    setTimeout(ch4, nextCallDelay);
}

OTHER TIPS

How about you return something instead so you don't have that huge stack of interconnected functions ?

function ch1():void {
   return true
}

 function ch2():void {
   return true
}

function myAnim(){
      var ani1 = ch1();
      var ani2 = ch2();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top