in Programming

ActionScript Animation

Kosal had a problem pausing between tweens. We went through several revisions.

First I suggesting defining the tween properties via array and using anonymous functions passed to the setTimeout function.

We found out anonymous functions were a no-go in his version of ActionScript. Then, I suggested using globals, which resulted in the following code:

// globals

var g_mc,
    g_a,
    g_b,
    g_c;

function animate () {
    var arr = [ 
            // mc, slideto vars, timeout
            [ car, 200, 100, 1, 10 ], 
            [dog, 200, 100, 5, 5]
        ];

var timeout = 0;
for ( var i = 0; i < arr.length; i ++ ) {
    var item = arr [ i ];
    g_mc = item [ 0 ];
    g_a = item[1];
    g_b = item[2];
    g_c = item[3];

    timeout += item[4]; // total seconds to timeout

    setTimeout ( doSlide, timeout );
}


}

function doSlide () {
    g_mc.slideTo ( g_a, g_b, g_c );
}
</code>

That had a problem with it, as when I was visualizing the code, I forgot that the globals would always point to the last array. We finally wound up with the code below, which uses recursion instead. 
<code lang="actionscript">
// globals

var g_index = 0,
 g_arr = [];

function animate () {
 g_index = 0;
        g_arr = [ 
 // mc, slideto vars, timeout
 [ car, 200, 100, 1, 10 ], 
 [dog, 200, 100, 5, 5]
 ];
        doSlide ();
}

function doSlide () {
        if ( g_index < g_arr.length  ) {
 var item = g_arr [ g_index ];
 g_mc = item [ 0 ];
 g_a = item[1];
 g_b = item[2];
 g_c = item[3];

g_mc.slideTo ( g_a, g_b, g_c );
            g_index ++;
         setTimeout ( doSlide, item[4] ); 
 }
}