开发者

Actionscript 3.0: Do I have to abort the previous tween if I start a new one with the same Movieclip?

开发者 https://www.devze.com 2023-03-26 06:13 出处:网络
I have this tween transition. Everytime button X is clicked it moves the movieclip (rectangle) a bit further (varx + numberx).

I have this tween transition. Everytime button X is clicked it moves the movieclip (rectangle) a bit further (varx + numberx).

var myTween:Tween = new Tween(rectangle, "x", Strong.easeOut, currentposx, varx, 3, true);

Somehow if I spam button X my computer become slow after a while. But somehow I can't find the source of the problem. My guess is that everytime I override the tween transition the pr开发者_运维问答eviously one is still intact somehow.

So do I have to abort a tween if it's overwritten with another one?


Using your button never should slow down the application. So there is something wrong.

  • A) Its your code
  • B) Its their code (Tween)

So what could happen:

- Time 0 sec - Button click - 1. tween starts, duration 3 sec.
- Time .1 sec - Button click - 2. tween starts, duration 3 sec.
- Time .2 sec - Button click - 3. tween starts, duration 3 sec.

We start new tweens in an interval of .1 sec. After 3 sek. we have then 30 tweens running at the same time.

At that time the first tween should end, unregister itself from any events dispatched by display objects (enter frame) or timers. Apparently it does not and your application adds tween by tween, neither is finishing.

It could be that your tween checks the distance between currentposx and varx and only if both are equal, the tween stops. Since there are now concurrent tweens, this situation may never happen, and your tween continues trying to move your display object again and again.

If you would show complete running code, I could test this in a profiler.

--

Anyway, you should store the current tween in a persistent variable such as _currentTween and either kill the tween before starting a new one or simply update the current tween's values when clicking the button again:

private var _currentTween : Tween;

private function buttonClickedVersionA() : void {
    if (_currentTween) _currentTween.stop(); // or whatever it is called
    _currentTween = new Tween(...);
}

private function buttonClickedVersionB() : void {
    if (!_currentTween) {
        _currentTween = new Tween(...);
    } else {
        _currentTween.start = currentposx;
        _currentTween.end = varx;
        _currentTween.restart(); // or whatever
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号