Problem with multiple sprites and hide() show()

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Karol Burdziński

I have a problem with this approach. I have a sprite for walking and sprite for idle (both have different spritesheets attatched).
Using AnimationPlayer and AnimatrionTree (with BlendSpace1D) I’m trying to animate the player to walk into left or right. The problem is when I’m showing and hiding proper sprite. It seems that the “old” animation is “flashing” right before the animation tree updates it.
Little explanation:

  • idle animation facing right
  • moving player to the right ( walking sprite is shown and walk right animation is ran)
  • stoping player, so idle animation facing right statrs
  • moving player to the left (walking sprite is shown again but walk left animation is ran)
  • stoping player ( now idle left animation is played, but right before there is a quick flash of the idle right animation).
    My code:
func _physics_process(delta):
var inputVector = Vector2.ZERO
var acceleration = ACCELERATION_VAL * delta
var friction = FRICTION_VAL * delta
inputVector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
inputVector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
inputVector = inputVector.normalized()
	
if inputVector != Vector2.ZERO:
	animationTree.set("parameters/Idle/blend_position", inputVector.x)
	animationTree.set("parameters/Walk/blend_position", inputVector.x)
	animationState.travel("Walk")
	spriteIdle.hide()
	spriteWalk.show()
	velocity = velocity.move_toward(inputVector * MAX_SPEED, acceleration)
else:
	animationState.travel("Idle")
	spriteIdle.show()
	spriteWalk.hide()
	velocity = velocity.move_toward(Vector2.ZERO, friction)
velocity = move_and_slide(velocity)
:bust_in_silhouette: Reply From: rathziel

Hi Karol,

I am by no means an expert here. But I am curious why you are using the hide() function for your animations. I don’t believe that is necessary.

I’ll start by recommending Heartbeasts tutorial here it’s fantastic and should give you a good starting point.

As for your script. It looks to be over complicated. But I guess it depends on what you are after. This is a snippet from Ben’s tutorial:

const SPEED = 200
var motion = Vector2()

if Input.is_action_pressed("ui_right"):
    	motion.x = SPEED
    	$Sprite.flip_h = false
    	$Sprite.play("run")
elif Input.is_action_pressed("ui_left"):
    	motion.x = -SPEED
    	$Sprite.flip_h = true
    	$Sprite.play("run")
else:
    	motion.x = 0

I’m not entirely sure how you would implement this into your code…but maybe it will help? ¯_(ツ)_/¯

Oh I was also following Heartbeasts tutorials but for rpg. Actually my character can move in all 8 directions, but by design character is only animated to left and right (like in old brawl games ). Anyway your solution might work, but I was trying to avoid flipping sprites
as later on I might have a lot of them + I would need to somehow flip the collision boxes also :smiley: But thanks for the help !: ).

Karol Burdziński | 2020-10-02 06:28