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