I’m having trouble with my game again, I have an action in which if you press the up key, the character swings a sword, I want it to be that if you are moving, you stop in place whilst the animation plays and continue on afterwards.
Moving up or down, it works as expected, but when moving left and right, the player just continues walking whilst swinging and i am stuck as to why. This is my code, it is the exact same for up and down as for left and right:
if action_id == hash("sword") then
if self.vel.y ~= 0 or self.vel.x ~= 0 then
walking = true
else
walking = false
end
if direction == "up" then -- checks what direction the player is facing
self.vel.x = 0 -- sets their x speed to 0
self.vel.y = 0 -- sets their y speed to 0
msg.post(".", "release_input_focus") -- stops the player from moving while the animation plays
sprite.play_flipbook("#sprite", hash("NU_Attack"), function() -- plays the attack animation
msg.post(".", "acquire_input_focus") -- allows the player to move again
if walking == true then
sprite.play_flipbook("#sprite", hash("NU_Walk")) -- Changes Animation to walk
else
sprite.play_flipbook("#sprite", hash("NU_Idle")) -- plays the idle animation
end
end)
elseif direction == "down" then -- checks what direction the player is facing
self.vel.x = 0 -- sets their x speed to 0
self.vel.y = 0 -- sets their y speed to 0
msg.post(".", "release_input_focus") -- stops the player from moving while the animation plays
sprite.play_flipbook("#sprite", hash("ND_Attack"), function() -- plays the attack animation
msg.post(".", "acquire_input_focus") -- allows the player to move again
if walking == true then
sprite.play_flipbook("#sprite", hash("ND_Walk")) -- Changes Animation to walk
else
sprite.play_flipbook("#sprite", hash("ND_Idle")) -- plays the idle animation
end
end)
elseif direction == "left" then -- checks what direction the player is facing
self.vel.x = 0 -- sets their x speed to 0
self.vel.y = 0 -- sets their y speed to 0
msg.post(".", "release_input_focus") -- stops the player from moving while the animation plays
sprite.play_flipbook("#sprite", hash("NL_Attack"), function() -- plays the attack animation
msg.post(".", "acquire_input_focus") -- allows the player to move again
if walking == true then
sprite.play_flipbook("#sprite", hash("NL_Walk")) -- Changes Animation to walk
else
sprite.play_flipbook("#sprite", hash("NL_Idle")) -- plays the idle animation
end
end)
elseif direction == "right" then -- checks what direction the player is facing
self.vel.x = 0 -- sets their x speed to 0
self.vel.y = 0 -- sets their y speed to 0
msg.post(".", "release_input_focus") -- stops the player from moving while the animation plays
sprite.play_flipbook("#sprite", hash("NR_Attack"), function() -- plays the attack animation
msg.post(".", "acquire_input_focus") -- allows the player to move again
if walking == true then
sprite.play_flipbook("#sprite", hash("NR_Walk")) -- Changes Animation to walk
else
sprite.play_flipbook("#sprite", hash("NR_Idle")) -- plays the idle animation
end
end)
end
end
If needed I can zip my project and send it if a closer look is needed to find the issue.