Godot Version
Godot 4.4
Question
I want to play animation while holding my left mouse, but i did smth wrong and my player don't even moove also then animation isn't worknig properly!
extends CharacterBody2D
var speed = 100
var friction = 0.1
var acceleration = 0.1
var is_chopping = false
@onready var sprite = $AnimatedSprite2D
@onready var trees: TileMapLayer = $"../Trees"
var last_direction = Vector2.DOWN
var tree_oak_id = 1
var tree_pine_id = 2
var chopped_oak = 3
var chopped_pine = 4
func _physics_process(delta):
if Input.is_action_pressed("chop"):
if !is_chopping:
is_chopping = true
sprite.play("chop")
print("clicked")
else:
if is_chopping:
is_chopping = false
print("stopped")
if not is_chopping:
var direction = Input.get_vector("left", "right", "up", "down")
if direction.length() > 0:
velocity = lerp(velocity, direction.normalized() * speed, acceleration)
if direction.x > 0:
sprite.flip_h = false
sprite.play("run")
if direction.x < 0:
sprite.flip_h = true
sprite.play("run")
if direction.y > 0:
sprite.play("run")
if direction.y < 0:
sprite.play("run_up")
else:
velocity = lerp(velocity, Vector2.ZERO, friction)
if not sprite.is_playing() or sprite.animation != "idle":
sprite.play("idle")
move_and_slide()
func chop_tree_at(world_pos: Vector2):
var cell_pos: Vector2i = trees.local_to_map(world_pos)
var source_id = trees.get_cell(cell_pos)
match source_id:
tree_oak_id:
trees.set_cell(cell_pos, chopped_oak)
tree_pine_id:
trees.set_cell(cell_pos, chopped_pine)
You’ve got everything in _physics_process()
indented inside the if Input.is_action_pressed("chop")
test.
At the very least, the move_and_slide()
at the end should be up a level of scope.
This is one of many reasons I’m Not A Fan of whitespace-delimited scope.
1 Like
okay i made that to be liek that because it was working like this, i was clicking with my mouse and then there was aniamtion but i wanted it to be with holding, so how to realise that ?
I think something like:
extends CharacterBody2D
var speed = 100
var friction = 0.1
var acceleration = 0.1
var is_chopping = false
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var trees: TileMapLayer = $"../Trees"
var last_direction = Vector2.DOWN
var tree_oak_id = 1
var tree_pine_id = 2
var chopped_oak = 3
var chopped_pine = 4
func _physics_process(delta):
# Check if the player is trying to chop...
if Input.is_action_pressed("chop"):
if !is_chopping:
is_chopping = true
sprite.play("chop")
print("clicked")
elif is_chopping: # "chop" isn't pressed, but we're chopping...
is_chopping = false
print("stopped")
# If we're not chopping, are we moving?
if not is_chopping:
var direction = Input.get_vector("left", "right", "up", "down")
if direction.length() > 0: # Are we trying to move?
velocity = lerp(velocity, direction.normalized() * speed, acceleration)
if direction.x > 0:
sprite.flip_h = false
sprite.play("run")
if direction.x < 0:
sprite.flip_h = true
sprite.play("run")
if direction.y > 0:
sprite.play("run")
if direction.y < 0:
sprite.play("run_up")
else: # Direction length is zero, slow down and idle.
velocity = lerp(velocity, Vector2.ZERO, friction)
if not sprite.is_playing() or sprite.animation != "idle":
sprite.play("idle")
# Move!
move_and_slide()
func chop_tree_at(world_pos: Vector2):
var cell_pos: Vector2i = trees.local_to_map(world_pos)
var source_id = trees.get_cell(cell_pos)
match source_id:
tree_oak_id:
trees.set_cell(cell_pos, chopped_oak)
tree_pine_id:
trees.set_cell(cell_pos, chopped_pine)
1 Like
I added some little changes, and now it works fine. Thanks for help, really appreaciate it!
1 Like