0 votes

Hi, all! I was wondering if there was an efficient way to make a one-framed animation play at the beginning, and another after colliding with a kinematic body 2D (The animated sprite is on an Area2D)? I originally had both frames in their own animations, but neither of them worked. I then wondered if I should put them in one animation, and just have the animation run to switch the frame. Another thought was to have two completely different sprites, and destroy the unnecessary one after the collision.

So far, I'm testing working with one animation and two frames. None of the codes I've tried even changes the frame- it's stuck on the first one. Here's the code so far.

extends Area2D

export var connected = false

# Called when the node enters the scene tree for the first time.
func _ready():
    $AnimatedSprite.animation = "switch"
    $AnimatedSprite.set_frame(0)


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
    #pass

func _on_Area2D_body_entered(body):
    $AnimatedSprite.animation = "on"
    connected = true
    $AnimatedSprite.set_frame(1)
in Engine by (26 points)

1 Answer

0 votes

I finally got it to work. Here's the code I use, in case someone else might be able to use it.

    extends Area2D

    export var connected = false

    # Called when the node enters the scene tree for the first time. 
func _ready():  $AnimatedSprite.animation = "switch"    $AnimatedSprite.set_frame(0)



func _on_Area2D_body_entered(body):
    if body.name == "Object1" or body.name == "Object2":
        connected = true
        $AnimatedSprite.set_frame(1)
by (26 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.