[ASFlash8] Recursivity, scope and onMotionEnd problem

Lo folks,

I’m getting an weird problem with an as class that i’m writing. The idea is simple, to have 10 bars changing their _yscale random.
Problem starts when i need to call the animate function the second time. It should be a very simple thing that i’m not seeing. Searched the web for several hours looking for a solution, found the delegate class and onther ones but yet i’m not getting it :sleep:.

Here’s the code:


import mx.transitions.Tween;
import mx.transitions.easing.*;
class SoundControler extends MovieClip {
    
    private var b_1_mc:MovieClip;
    private var b_2_mc:MovieClip;
    private var b_3_mc:MovieClip;
    private var b_4_mc:MovieClip;
    private var b_5_mc:MovieClip;
    private var b_6_mc:MovieClip;
    private var b_7_mc:MovieClip;
    private var b_8_mc:MovieClip;
    private var b_9_mc:MovieClip;
    private var b_10_mc:MovieClip;
    private var active:Boolean;
    function SoundControler() {
        
    }
    
    public function onLoad() {
        this.stopSound();
    }
    
    public function stopSound() {
        var z:Number;
        var orig:Number;
        var dest:Number;
        var ref_mc:MovieClip;
        this.active = false;
        for(z=1;z<11;z++) {
            ref_mc = this["b_" + z + "_mc"];
            orig = ref_mc._yscale; 
            dest = 100 - z*10;
            new Tween(ref_mc, "_yscale", None.easeOut, orig, dest, 10, false);
        }
    }
    
    public function startSound() {
        var z:Number;
        this.active = true;
        for(z=1;z<11;z++) {
            this.animeBar(z);
        }
    }
    
    private function randRange(min:Number, max:Number):Number {
        var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
        return randomNum;
    }

    public function animeBar(bn:Number) {
        var mt:Tween;
        var orig:Number;
        var dest:Number;
        var ref_mc:MovieClip;
        if(this.active) {
            ref_mc = this["b_" + bn + "_mc"];
            orig = ref_mc._yscale; 
            dest = this.randRange(0,100);
            mt = new Tween(ref_mc, "_yscale", None.easeOut, orig, dest, 5, false);
            mt.onMotionFinished = function() {
                // the folowing call won't work
                animeBar(bn);
            }
        }
    }
}

The problem is in the onMotionFinished call, i think the scope of the call isn’t the same, i mean, animeBar(bn) isn’t in the same scope as the class but i don’t know how to solve it.

Any help, hints, whatever would be very apreciated!

Sorry for my bad english.