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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 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] );
 }
}