How do I make a PackedScene plays an specific Animation

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

I have a PackedScene that loads an AnimatedSprite Scene playing Animation 1.
I want PackedScene, however, to play Animation 1, but after a while changes to play Animation 2;

I tried the basics with PackedScene.play(“Animation2”) function, but returned saying that it is a non-existent function in base PackedScene.

What is the best method to do this animation change in an AnimatedSprite from a PackedScene?

Thanks guys

:bust_in_silhouette: Reply From: Jowan-Spooner

Hi lucasfazzi,
PackedScene is a resource mainly used to have a scene loaded that isn’t used yet. If you use your scene you should instance it like this:

export (PackedScene) my_packed_scene = preload("res://Whatever.tscn")

func some_function():
    # create a new instance of the packedscene
    var new_whatever = my_packed_scene.instance()

    # if the root node of the packed scene is the Animationplayer
    new_whatever.play("Animation1")

    # otherwise
    new_whatever.get_node("AnimationPlayer").play("Animation1")
    
    # add the newly created "node" as a child in your running scene
    add_child(new_whatever)

Hope it helps, good luck!

Hi Jowan;

Thank you for clarifying;

Worked as well.

Thanks

lucasfazzi | 2019-05-29 17:25