How to Open ad close door in 2D

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

when i open the door is working. but i cant close it anymore.

extends Area2D

var bodyNearDoor = false

onready var animationDoor = $AnimationDoor


func _process(delta):
if Input.is_action_just_pressed("Interact"):
	if bodyNearDoor == true :
		animationDoor.play("Door_Open")
		
	if animationDoor.play("Door_Open") == true :  <---- This doesnt work
		animationDoor.Play("Door_Close")

func _on_Door_body_entered(body):
if body.name == "Player":
	bodyNearDoor = true


func _on_Door_body_exited(body):
if body.name == "Player":
	bodyNearDoor = false
:bust_in_silhouette: Reply From: LoneDespair

If you’re using an AnimatedSprite2D, otherwise pls let us know

if animationDoor.animation == "Door_Open":
   animation.play("Door_Close")

no i use sprite and animationplayer

szccornish | 2021-02-24 11:06

if animationDoor.assigned_animation == "Door_Open"
    animationDoor.play("Door_Close")

LoneDespair | 2021-02-24 11:15

:bust_in_silhouette: Reply From: whiteshampoo

(i guess animationDoor is an AnimationPlayer)

You need to save the “state” of the door.
You also might to check if the other animation is still playing.

#[...]

var is_open : bool = false

func _process(_delta : float) -> void:
    if Input.is_action_just_pressed("Interact"):
        if bodyNearDoor and not animationDoor.is_playing():
            if not is_open:
                animationDoor.play("Door_Open")
                is_open = true
            else:
                animationDoor.Play("Door_Close")
                is_open = false

#[...]

(untested)

:bust_in_silhouette: Reply From: Inces

The part that you indicated as not working is using wrong built-in function.
It should be :
if animationdoor.is-playing() == true and animationdoor.current-animation == “Door Open”