[SOLVED] Issue playing animation -- possibly being overridden?

Hi all. I’m new to Defold but enjoying it and the learning process. I’ve run into an issue where one of my animations for the player’s melee attack, to which I’ll need eventually to attach a collision object so that it can interact with enemies, is not playing. It seems that my walk animation is overriding it but I’m not sure how to correct that. Currently, when either of the attack keys are pressed, instead of that animation triggering, my character sprite moves left with the relevant walk animation.

I can see, printing to the console, that the correct key bindings are registered, but the correct animation isn’t firing in response. Interestingly–I’m not sure why this is but I suspect its in the way things are passed between code segments, and its part of what led me to believe the animation is being overridden–when I call #sprite rather than self in my on_input attack play_animation statement, the attack animation does briefly play before the sprite moves left and the walk animation is triggered. But my understanding is that self should be used in this instance rather than #sprite, no?

Any help would be much appreciated!

This is the code I’ve got at the moment for my animations (for context: based on https://www.youtube.com/playlist?list=PL4_orbQ0JeQtynHimL-r8DUqH5Jf_RHZ2).

function play_animation(self, new_animation)
	if self.animation ~= new_animation then
		sprite.play_flipbook("#sprite", new_animation)
		self.animation = new_animation
	end
end

function animate(self) 
	if self.ground_contact then -- if in contact with the ground and not moving, do nothing
		if self.velocity.x == 0 then
			play_animation(self, "idle")
		else
			play_animation(self, "walk") -- if in contact with the ground and moving, walk
		end
	end
end

function on_input(self, action_id, action)
	if action_id == hash("attack") and action.pressed then
		print("Attack action pressed")
		play_animation(self, "alice_attack")
	else
		if action_id ~= hash("jump") then
			self.direction = (action_id == hash("right")) and DIRECTION_RIGHT or DIRECTION_LEFT
			walk(self)
			flip(self.direction)
		elseif action.pressed and self.ground_contact then
			self.velocity.y = JUMP_TAKEOFF
			self.ground_contact = false
		end
	end
end

You haven’t shown all the code (especially the update function), but it is most likely that the attack animation is getting overridden like you said.

When the player attacks, set a flag like self.attacking = true and don’t play the idle or walking animation while it’s set. Then, use a timer or the complete_function parameter of sprite.play_flipbook() to set self.attacking = false when the attack finishes.

I think when you press attack key, there are pressed and unpressed triggered. And if it’s unpressed, it goes to your else case and by action_id is not “jump”, it walks. So I think replacing else to elseif action.pressed then would help

@Potota @Chung_Xa I’ve worked quite a bit with it and from your suggestions went on to try and managed to get the attack animation triggered and monitored correctly relative to the others with a state machine. Thanks for your help!

2 Likes