(solved)Struggling with Animation (coding) : 2D book open and close Anim

Godot Version

Godot_v4.2.1

Question

Hi guys. My question is like in the title I’m struggling making a 2d book closing and opening animation as a pause menu when the button is pressed.

Problem:
Using Animated sprite node or Animation node isn’t the problem the problem here is coding the animation for it to work directly in the game. I could code the sprite for it to appear but I can’t seem to make it identify when to play the closing animation or opening animation and when to stop the closing animation.

here’s my code:

func _process(delta)
if Input.is_action_just_pressed(“Tab”):
$AnimatedSprite2D.play(“book_open”) #“book_open/close” is the Animsprite2D
elif Input.is_action_just_pressed(“Tab”):
$AnimatedSprite2D.play(“book_close”)
(Idk how to end the closing animation even if I turn off the looping button. I’ve already used $AnimatedSprite2D.stop and it doesn’t work)

extra comment:
my code sucks I know I just started a month ago (feel free to criticize me) it’s simple and I’m still learning.

why it’s in process(delta)? I’m only following a tutorial and I can’t seem to make a function for an animation. I won’t tell what tutorial I don’t want the blame to be put on them for my stupidity.

I’ve been struggling with this for a week now, I’ve tried searching but nothing seems to work. if anyone knows a tutorial or a way for this to work. please lend a hand. I’m not quite familiar with coding, I only know the basics of GDScript and that’s it. I’m making my first game and in the process learn coding. (why not just use a simplier way? it’s because it’s part of the theme of my game. I really want it to work and I don’t want to scrape my already planned game). also if you guys know helpful tips on coding feel free to comment it.

why it’s in process(delta)?

The _process function runs every frame. The code is in the _process function so that, every frame, that game will check whether the player has pressed the tab button, and if so, play the animation.

I can’t seem to make it identify when to play the closing animation or opening animation

Right now, you have an if-statement with an elif-branch, that both have the same condition. The way elif works, though, is that it only checks the condition if the if branch wasn’t taken. So right now, your elif branch will never be used.

My suggestion is to add a boolean variable to your script, that tracks whether the book is currently open, and use that to decide which animation to play:

// If your book is closed when the game starts, change this to false
var is_open = true

func _process(delta):
    if Input.is_action_just_pressed("Tab"):
        if is_open:
            $AnimatedSprite2D.play("book_close")
            is_open = false
        else:
            $AnimatedSprite2D.play("book_open")
            is_open = true

That is, whenever the user presses tab, we look at the state of the is_open variable, and decide what to do: If the book was already open, play the close animation and flip the variable to false to indicate that the book is now closed. Otherwise, play the open animation, and flip the variable to true to indicate that the book is now open.

1 Like

Thank you so much. it’s fixed now. you da best.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.