Tween good practices

Hi, I recently started to wonder if if I use the destroy method on the object to which tween is connected:

const tween = this.tweens.add ({targets: object,

})

obj.destroy()

Is there a need to remove this tween afterwards?

tween.remove ()

Does this have any effect on performance?

Tweens are automatically cleaned up by the Tween Manager after they complete. Destroying a Game Object after you no longer need it is a good idea - it’s faster (because you’re not rendering objects that won’t ever be seen anymore) and it saves memory (because the browser can garbage-collect the object). Note that there’s no direct connection between destroying a tween (which is done automatically) and destroying a Game Object.

2 Likes

I forgot that tween is looped so

repeat:-1

Is tween with this property cleaned up when his target is destroyed?

It’s not. You have to stop the tween yourself with tween.stop().

sorry for necrobumping this.

but does stopping tween automatically remove it as well ?
i am having a tween that need to be replaced when resize event is called (for responsive purpose)

is doing this fine ?

const tween =this.tween.add(. . . )
this.scale.on('resize', () => 
{
  tween.stop();
  tween = this.tween.add(. . . )
})

It does, but not right away. In practice it may make little difference. You could use remove() there.

stop()

Stops the Tween immediately, whatever stage of progress it is at and flags it for removal by the TweenManager.

remove()

Immediately removes this Tween from the TweenManager and all of its internal arrays, no matter what stage it as it. Then sets the tween state to REMOVED .

1 Like