문제

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"));
}
도움이 되었습니까?

해결책

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);
}

다른 팁

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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top