Problem with Tween and adding multiple instances in a scene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Judosii

Hey guys, I have a scene with Sprites I want to animate with Tweens, because what I’m trying to do is making a sprite appear off-screen,both Sprites slide to the right and the newly spawned sprite stops over where the last one was, then queue_free() the old sprite. Also the Sprites share the same name, except for the number at the end which is a variable called “menu”, that way I can call both sprites for a different tween animation, and queue free() the old Sprite without any fear of deleting the wrong sprite.

add_child(img)
img.name = str("menu",previous_menu)
img.position.x = -1024
img.texture = preload("res://sprites/UI/Pause menu/template.png")
img.hframes = 4
img.frame = previous_menu - 1
tween.interpolate_property(img, "position:x", 0,1024,0.7, Tween.EASE_IN_OUT, Tween.TRANS_SINE)
tween.interpolate_property(img, "position:x", -1024, 0,0.7, Tween.EASE_IN_OUT, Tween.TRANS_SINE)
tween.start()
yield($Tween,"tween_all_completed")

After this code, the old Sprite queue_frees() and the variable menu is changed so that the new sprite now bears the name of str(“menu”,menu), so that I can repeat it

That’s what’s supposed to happen, but it either queue_free()s the newer sprite, or just crashes because it can’t find a node with the name of str(“menu”,menu).

And some sprites can’t spawn because it says "can’t add child ‘menu3’ to ‘pause_menu’, already has a parent ‘pause_menu’ " .

Do any of you know why it’s not working ? If so, thank you in advance, and sorry for making this such a long post!

:bust_in_silhouette: Reply From: Inces

Name is a built-in property that is always unique and changing it into something that already exists will fail. First You have to queue_free old Sprite, and only then your new Sprite can be renamed to removed ones name.

The other error You are getting happens when You add_child two the node that already has parent. Which means You call it twice on one node. Maybe it is also because of the name issue. You can show the code what happens before adding child

WHatever You are planning to do sounds overengineered :P. Do You know about existence of Control Nodes ( Buttons, Containers, Panels ) ? :wink: