How to get AnimatedSprite2D to change 1 frame per key press

Godot Version

4.2.2

Question

` Hello! I’m incredibly new to Godot and I’m near confused at every turn! Currently, I am trying to make a menu where a AnimatedSprite2D will change its appearance in responde to the A and D keys, and it will change through a list that will expand as I work on my little project more.

Currently, my major issue is that I can’t find a way to make the Animated Sprite 2D’s frame counter tick up or down. The code I have is:

extends AnimatedSprite2D

func _ready():
pass

func _process(delta):
pass

func _input(IconShift):
if Input.is_key_pressed(KEY_D):
$AnimatedSprite2D.frame(1)
elif Input.is_key_pressed(KEY_A):
$AnimatedSprite2D.frame(0)

The reason I want to do it through a variable rather than changing the image name is because I want it to be easy for me to add in new icons later by just placing into my spritesheet. Earlier I had something like “$AnimatedSprite2D.frame = $AnimatedSprite2D.frame + 1:” but nothing I’ve tried seems to work. Anything I’ve tried to mathematically increase or decrease the frame of the sprite has resulted in “Attempt to call function ‘frame’ in base ‘null instance’ on a null instance.”
I have no clue what this means or how to fix it.

TLDR/Clarification: What is the easiest way to have a sprite change it’s appearance based on a spritesheet by just one frame at a time?

Your script extends an AnimatedSprite2D and in the input function you access a child node of type AnimatedSprite2D (“null instance”). That looks bad.
I think using an animation for this is more complicated than necessary. Why don’t you try it with an AtlasTexture and change the region with each click?

Ill see if I can try that, that sounds like it will do what I want. Thanks!
Also I agree, I swapped off an Animated Sprite figuring there was easier ways to do it. Thanks so much!

I think so, but it doesn’t hurt to know the fault in your code:
Since your script is extended from AnimatedSprite2D I guess you’ve connected it to the same node, One thing you need to know is that the path of the nodes from the node connected to the script is specified, so you need to point to $“.” to point to your AnimatedSprite2D, in which case your code bug will be fixed.
One more point: Attempt to call function ‘frame’ in base ‘null instance’ on a null instance. Phrases like this indicate that you have either not identified your object(For example, a node) correctly or have pointed out a specification that does not exist