What's the best way to tween a sprite's scale?



  • This is simple and attractive:

    FlxTween.tween(this.scale, {x: 0, y: 0}, 0.5);
    

    However, it is also subtly buggy. If the sprite gets destroyed before the tween is over, it will release all its properties back into their respective pools, including the FlxPoint that is this.scale. If some other sprite then recycles this FlxPoint while the tween is still running, this causes very hard to debug issues.

    A solution would be to store all tweens, and cancel them in a destroy() override. But I don't like it; it's extra code and easy to forget. Any better ideas?

    Come to think of it, the same might be a problem with recycling FlxSprite itself as well, for instance when tweening alpha.



  • The first thing I could think of is adding a onUpdate callback for every tween. Like this:

    FlxTween.tween(this.scale, {x: 0, y: 0}, 0.5, { onUpdate:stopIfSpriteDestroyed });
    
    function stopIfSpriteDestroyed(tween:FlxTween) {
        if (!this.exists)
            tween.cancel();
    }
    

Log in to reply