Having trouble figuring out stop/idle animations (anim8/hump.timer/boipushy)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
AlanGmnz
Prole
Posts: 8
Joined: Wed Nov 08, 2017 9:25 pm
Location: Brazil

Having trouble figuring out stop/idle animations (anim8/hump.timer/boipushy)

Post by AlanGmnz »

I've been trying to come up with a simple stop/idle cycle for a sprite for hours and I can't seem to figure out what I'm missing.

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
jdse03
Prole
Posts: 18
Joined: Sun Oct 01, 2017 4:45 pm

Re: Having trouble figuring out stop/idle animations (anim8/hump.timer/boipushy)

Post by jdse03 »

.
Last edited by jdse03 on Wed Aug 03, 2022 12:53 am, edited 1 time in total.
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Having trouble figuring out stop/idle animations (anim8/hump.timer/boipushy)

Post by yetneverdone »

Uaing anim8: call the animation update only when your pressing the move key.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 39 guests