question about playing animation

zxwtzxwt Posts: 4Member
edited December 1969 in 2D
I add a sprite inside a rigidbody representing the player.<br />I try to play the walking animation while using the move() function.<br />The character moves forward without the animation, I know that the animation stuck at the first frame because I use the _fixed_process function.<br />My code end up with something like this:<br />
var anim = &quot;&quot;<br />var anim_new = &quot;&quot;<br /><br />func _ready():<br />	# Called every time the node is added to the scene.<br />	# Initialization here<br />	set_fixed_process(true)<br />	<br />func _fixed_process(delta):<br />	play(delta)<br />	<br />	if anim != anim_new:<br />		anim_new = anim<br />		get_node(&quot;AnimationPlayer&quot;).play(anim)<br /><br />func play(delta):<br />	anim = &quot;idle&quot;<br />	if Input.is_action_pressed(&quot;ui_accept&quot;):<br />		anim = &quot;run
<br /><br />I get this idea from the platformer demo but I don't really understand it.<br />I know the animation will only play when the animation is different.<br />Isn't anim and anim_new will become the same after the first loop?<br />Why would it keep playing the idle animation?<br />I need some explanation to understand the loop. :-\<br />

Comments

  • danjodanjo Posts: 81Member
    The animation player will always play the same animation, even if "ui_accept" is pressed because anim_new always keeps it's initial value. In the worst case this means that no animation is played at all (or maybe a chrashing game, not sure what happens if you pass null to AnimationPlayer.play()).<br /><br />The code should look like this:<br />
    <br /><br />var anim = &quot;&quot;<br />var anim_new = &quot;idle&quot;<br /><br />func _ready():<br />	# Called every time the node is added to the scene.<br />	# Initialization here<br />	set_fixed_process(true)<br />	<br />func _fixed_process(delta):<br />	play(delta)<br />	<br />	if anim != anim_new:<br />		anim_new = anim<br />		get_node(&quot;AnimationPlayer&quot;).play(anim)<br /><br />func play(delta):<br />	anim_new = &quot;idle&quot;<br />	if Input.is_action_pressed(&quot;ui_accept&quot;):<br />		anim_new = &quot;run&quot;<br />
    
    <br />
  • zxwtzxwt Posts: 4Member
    You misunderstand something, my code actually works. When the game starts, the idle animation plays and when I press "ui_accepted", the run animation plays. I want to know why it works.
  • danjodanjo Posts: 81Member
    You  are right, i misread the code.<br /><br />Maybe this helps.<br /><br />When _fixed_process() is called the first time, the 'if ..' is always true, no matter if a ui_accept is pressed or not. Let's say it's not pressed so anim gets set to "idle", anim_new is still set to "", so the if evaluates to true ("idle" != ""), so the "idle" animation plays and anim_new is set to anim.<br />As long as ui_accept is not pressed the idle animation keeps playing, because "idle" != "idle" evaluates to false.<br />If ui_accept is pressed, anim gets set to "run", so the if is again true ("run" != "idle") the run animation plays and anim_new is set to anim.<br />As long as ui_accept is pressed, anim and anim_new will both be set to "run", the moment it's not pressed anim gets set to "idle" again but anim_new is still set to "run" so the if evaluates to true ("idle" != "run").<br /><br />Hopes that makes it clear, it could also help to add some print()'s to see what's going on.<br />I guess what confused me was that anim_new is not the new animation but the current animation.
  • zxwtzxwt Posts: 4Member
    Thx, I understand now, so I guess the animation will not stop until I tell it to stop. That's why it will continue to loop when I enable looping.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file