- Edited
So earlier, I talked about why you should have one script do one thing. So, does this mean we need to animate the player with the animated sprite?
Yes, of course! That would be an easy start in the right direction.
So, what might our animations for this maze game be?
Idle Walk.
Simple enough. Now, how do we animate something when the player moves? Signals?
Not exactly, there is an easier way to do this. =)
If you look back at the player, under get_input(), it shows what inputs cause the player to move. We can use those exact same inputs for our code in the animation player.
Let's add a script to the Animated Sprite, and copy/paste get_input. We also need to use func _process(delta) for this.
Now, we can remove everything about velocity, and move_and_slide, the animated sprite is not detecting collision. What do we need the code?
play("whatever_animation), like this:
Now... what about if we want it to stop?
Let's look back at the player's get_input(). What else does it have on it?
velocity = Vector2()...
What is that?
If you remember from our test, it kept the player from constantly moving. How?
Vector2() means Vector2(0, 0). If we are not pressing any buttons, velocity will always be 0, 0. This is why the character stops.
So, can we use it? I mean, the Animated sprite is the child, not the parent...
Normally, you would use a signal to get to a parent, but there is another way. Since I want to show you whatever I can, the other method is to write get_node(), and with the Player selected, drag it into those parentheses and see what you get.
Ignore the error for now. This is the child calling its parent directly through ode. Yes, they often use signals for this instead, but this was to show you that you can do it.
Now, how to check the velocity?
Every node grabbed in code can have all of their variables, functions, and whatever else used. You could add .velocity == Vector2(0, 0) right in.
Now we need to finish the if statement with play("idle_animation_name")
.
And you're done!
However, there is an even easier way to do this! :o
Here's my challenge for you in this round: You have all of the information on how to do this in this post. What can you do to basically turn all of this into two simple if statements?
I will see you in the next one.