I’m new to this kind of coding. I’ve coded with blocks and that’s all, though because of this I’m able to get smooth movement to work. However, I have no clue how to play animations. I’m making a farming game and am using the animated sprite 2d because I don’t know how animation players work.
extends StaticBody2D
func get_input():
var input = Vector2()
if Input.is_action_pressed(“Clicked”):
$AnimatedSprite2D.play(“growing.demo”)
This doesn’t work and I don’t know why. I added a hit box and it still wont work. Any clue how to fix this? appreciate it :D!
Ok i think i know the cause. You need to go to the project settings and set an input for left (set it to A) , right (set it to D) and jump (set it to space). Then create a new scene called Player and add a CharacterBody2D, then as a child, add a collision shape 2D and an animated sprite 2D and put the animation in here. Next create an empty sprite in the CharacterBody2D and put in this script:
extends CharacterBody2D
@export var SPEED = 300.0
@export var JUMP_VELOCITY = -400.0
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
var left := ("left")
if Input.is_action_just_pressed("left"):
$AnimatedSprite2D.play("growing.demo")
move_and_slide()
Then when you press A, it should play the animation. I hope this helped!
Ok, so you don’t need an animation-tree. Your code looks good, but I see two possible problems here:
Are you calling get_input somewhere? Maybe you mean to override the built-in _input?
As @pro_core3 suggests, did you specify “Clicked” in the input map?
Since your are new to coding, I suggest you look into how to debug your code.
A simple first step would be to add some print statements to see if the code is actually executed.
Even better would be to look into the debugging tools to analyze the execution of your code (Overview of debugging tools — Godot Engine (stable) documentation in English). Debugging is invaluable and will save you some trips to the forum
Also the AnimationPlayer is quite easy to grasp. You can find a great intro here: Animation — Godot Engine (stable) documentation in English.
In general I would suggest to look at the official documentation. There are great resources there to get you started.
for 1 I wasn’t sure how to answer, I just assumed I was supposed to use get_input because I was clicking it (?) not sure how much of this makes since, again, I’m new TToTT (i miiighhhtt have to go change my input actions…)
ooohhhh, thank you :D!!! this actually helps a lot oml (I have put get_input on like, 7 different codes (they don’t work) so Imma go fix that :3, thanks again :D!)