Here's the code with explanations for what I'm trying to achieve. Any insights?
Code: Select all
local anim8 = require 'bin/anim8'
local timer = require 'bin/timer'
local Input = require 'bin/input'
function love.load()
input = Input()
local sprite = love.graphics.newImage('assets/sprite.png')
local g = anim8.newGrid(40, 76, sprite:getWidth(), sprite:getHeight())
player = {
sprite = sprite,
x = 60,
y = 180,
speed = 50,
dir = true,
animations = {
walk = anim8.newAnimation(g('1-10',1), 0.1),
idle = anim8.newAnimation(g('1-15',2), 0.1),
}
}
input:bind('right', 'wright')
input:bind('left', 'wleft')
end
function love.update(dt)
-- Walk left - working
if input:down('wleft') and not input:down('wright') and player.x > 60 then
player.animation:resume()
player.animation = player.animations.walk
player.x = player.x - (player.speed*dt)
player.dir = false
player.animation:update(dt)
-- Walk right - working
elseif input:down('wright') and not input:down('wleft') and player.x < 420 then
player.animation:resume()
player.animation = player.animations.walk
player.x = player.x + (player.speed*dt)
player.dir = true
player.animation:update(dt)
-- If both keys are down, keep the walk animation going for 0.2 seconds and then pause - not working, animation just stops
elseif input:down('wleft') and input:down('wright') then
timer.after(0.2, function() player.animation:pause() end)
player.animation:update(dt)
timer.update(dt)
-- If a key is released, same behavior as above
elseif input:released('wleft') or input:released('wright') then
timer.after(0.2, function() player.animation:pause() end)
player.animation:update(dt)
timer.update(dt)
-- Idle cycle that should wait, play the animation, wait, pause and then loop - not working, first cycle goes well but then animation loops ignoring the wait calls
elseif not input:down('wleft') and not input:down('wright') then
player.animation = player.animations.idle
timer.script(function(wait)
wait(2)
player.animation:resume()
wait(2)
player.animation:pause()
player.animation:resume()
player.animation:update(dt)
end)
timer.update(dt)
end
end
function love.draw()
if player.dir then
player.animation:draw(player.sprite, player.x, player.y, 0, 1, 1, 20, 0 )
elseif not player.dir then
player.animation:draw(player.sprite, player.x, player.y, 0, -1, 1, 20, 0 )
end
end