Hi guys.
I am animating my player’s sprite in _process(delta):
When I press the left mouse it starts the attack animation and continues in loop for as long as I hold the mouse down.
So far so good.
The problem is that if I press in rapid succession the attack animation starts over again, it makes sense but it is not what I want.
I would like if it was possible to interpret multiple touches of the mouse as being held down.
So if I press the mouse multiple times or hold down, the attack animation continues in a loop without resetting.
When you click the mouse, 2 events are triggered: mouse button down and mouse button released.
I was running into the problem of my mouse click code running twice because it was running on both the down and the release.
To do what you’re looking for, check for down and release and execute where appropriate.
If you want to activate on the down and keep executing while it’s held, you can check for that and then end the action on the release.
Remember, _process(delta) execute repeatedly with time involved. My game is turn-based. I only execute when the player clicks the mouse, so that’s either _input() or a custom signal.
func _process():
if Input.is_action_pressed("LMB"):
if anim_player.is_playing():
if anim_player.current_animation != "attack":
anim_player.play("attack")
else:
anim_player.play("attack")
I cant say it will work or not.
It would be nice if you share code
I set a timer called “CanAttack” of 0.4 seconds because my animation takes 4 frames per second to complete.
func _process(delta):
# Either by holding down the mouse or by pressing repeatedly, the timer starts with a countdown of 0.4 seconds.
if Input.is_action_just_pressed("attack") or Input.is_action_pressed("attack"):
$CanAttack.start()
# If the player is not stationary and the timer is greater than 0, the run and attack animation starts.
if direction != Vector2.ZERO and $CanAttack.time_left > 0:
#play animation attack and run
else:
#play animation run no attack
I hope I have been clear and can be of help to someone.