Need help with anim8

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
subtledemise
Prole
Posts: 6
Joined: Tue Dec 10, 2013 1:34 am

Need help with anim8

Post by subtledemise »

I'm trying to play animation while the player is moving; however, I keep getting these error messages:
BHesdhs.jpg
BHesdhs.jpg (107.22 KiB) Viewed 342 times
My code:

Code: Select all

local anim8 = require 'anim8'
local player

function love.load()
local walk = love.graphics.newImage("image/player/male_walkcycle.png")
local g = anim8.newGrid(64, 64, walk:getWidth(), walk:getHeight())

player = {
	walk = walk,
	x = 200,
	y = 200,
	speed = 50,
	animations = {
		up = anim8.newAnimation(g('loop',1,'2-9'), 1.0),
		down = anim8.newAnimation(g('loop', '20-27',1), 1.0),
		left = anim8.newAnimation(g('loop', '11-18',1), 1.0),
		right = anim8.newAnimation(g('loop', '29-36',1), 1.0)
		}
	}
	player.animation = player.animations.down
end

function love.update(dt)
player.animation:update(dt)
-- Right
if love.keyboard.isDown("d") then
	player.x = player.x + player.speed * dt
    player.animation = player.animations.right
end
-- Left
if love.keyboard.isDown("a") then
	player.x = player.x - player.speed * dt
    player.animation = player.animations.left
end
-- Up
if love.keyboard.isDown("w") then
	player.y = player.y - player.speed * dt
	player.animation = player.animations.up
end
-- Down
if love.keyboard.isDown("s") then
	player.y = player.y + player.speed * dt
	player.animation = player.animations.down
end
end

function love.draw()
animation:draw(player.walk, player.x, player.y)
end
Am I doing something wrong? I have also attached a .love file for reference. Thanks in advance.
Attachments
need help.love
(28.17 KiB) Downloaded 111 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Need help with anim8

Post by Robin »

I don't know anim8, but shouldn't it be up = anim8.newAnimation(g('loop','2-9',1), 1.0),?
Help us help you: attach a .love.
subtledemise
Prole
Posts: 6
Joined: Tue Dec 10, 2013 1:34 am

Re: Need help with anim8

Post by subtledemise »

Ok I fixed up the code a lot. I was using some outdated tutorials. I'm now having a problem with my grids I think:
Error
anim8.lua:35: There is no frame for x=20,y=3

Code: Select all

local anim8 = require 'anim8'
local player

function love.load()
local walk = love.graphics.newImage("image/player/male_walkcycle.png")
local g = anim8.newGrid(64, 64, walk:getWidth(), walk:getHeight())

player = {
	walk = walk,
	x = 200,
	y = 200,
	speed = 50,
	animations = {
		up = anim8.newAnimation(g('2-9',1), 1.0),
		down = anim8.newAnimation(g('20-27',3), 1.0),
		left = anim8.newAnimation(g('11-18',2), 1.0),
		right = anim8.newAnimation(g('29-36',4), 1.0)
		}
	}
	player.animation = player.animations.down
end

function love.update(dt)
player.animation:update(dt)
-- Right
if love.keyboard.isDown("d") then
	player.x = player.x + player.speed * dt
    player.animation = player.animations.right
end
-- Left
if love.keyboard.isDown("a") then
	player.x = player.x - player.speed * dt
    player.animation = player.animations.left
end
-- Up
if love.keyboard.isDown("w") then
	player.y = player.y - player.speed * dt
	player.animation = player.animations.up
end
-- Down
if love.keyboard.isDown("s") then
	player.y = player.y + player.speed * dt
	player.animation = player.animations.down
end
end

function love.draw()
animation:draw(player.walk, player.x, player.y)
end
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Need help with anim8

Post by kikito »

Hi, anim8 creator here.
Robin wrote:I don't know anim8, but shouldn't it be up = anim8.newAnimation(g('loop','2-9',1), 1.0),?
That was the old anim8. On the new one there is no 'loop' param. (everything is 'loop', if you want 'once' or 'bounce' you can simulate them using the onLoop callback and the pause method).
anim8.lua:35: There is no frame for x=20,y=3
Your numbers were a bit off. The numbers inside the grid parameters are not "image coordinates", but "positions of frames". So 1 = first frame, 2 = second frame, etc. The pixels are calculated for you by the grid object (the g variable).

Since your image is a grid of 9x4 frames, but you are trying to create animations with access frames outside of that (x=20), you get that error.

This will work:

Code: Select all

    animations = {
      up     = anim8.newAnimation(g('2-8',1), 0.1),
      left   = anim8.newAnimation(g('2-8',2), 0.1),
      down   = anim8.newAnimation(g('2-8',3), 0.1),
      right  = anim8.newAnimation(g('2-8',4), 0.1)
    }
g('2-8', 1) means "Take the frames {x=2,y=1}, then {x=3,y=1} and so on until {x=8,y=1}". So '2-8' means "x moves from 2 to 8", and 1 means "y stays at 1".

I have also lowered the time from 1 to 0.1; otherwise the steps were waaaay too slow.

Also, in love.draw you used animation but you should use player.animation instead:

Code: Select all

function love.draw()
  player.animation:draw(player.walk, player.x, player.y)
end
Attaching working example.

Have fun!
Attachments
get_up_and_walk.love
(23.45 KiB) Downloaded 154 times
When I write def I mean function.
subtledemise
Prole
Posts: 6
Joined: Tue Dec 10, 2013 1:34 am

Re: Need help with anim8

Post by subtledemise »

Thanks for the help. I realized most of my mistakes laying in bed last night. I was taking something simple and making it overly complicated. I was thinking of each frame as individually numbered entities, like the top left as 1, and the row below as 10 -18 and so on. Anyway, thanks for the hints on the time.I noticed they were super slow, but the wife needed the computer and I had to get to work lo
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Need help with anim8

Post by kikito »

subtledemise wrote:I noticed they were super slow, but the wife needed the computer and I had to get to work lo
:ultraglee: yeah, that happens
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot] and 2 guests