actionscript-3tweener

ActionScript - Trace Update Value from Tweener


tweener doesn't update myValue while passing the param while tweening. why?

public var myValue:Number = 0.0;

Tweener.addTween(this, {myValue: 1.0, time: 2.0, onUpdate: traceValue, onUpdateParams: [myValue]});

private function traceValue(value:Number):void
    {
    trace(value);
    }

Solution

  • Primitive values are always passed by value in ActionScript, never by reference. Tweener is updating your value, but what gets passed to traceValue is always the original value. So in your code above it'll always trace out 0. The solution is to pass in a reference to the target object instead, and read the value each time. If you pass in the field name this can be done dynamically for the most flexibility. Eg:

    public var myValue:Number = 0.0;
    
    Tweener.addTween(this, {myValue: 1.0, time: 2.0, onUpdate: traceValue, onUpdateParams: [this, 'myValue']});
    
    private function traceValue(target:Object, field:String):void
    {
        trace(target[field]);
    }