Page 1 of 1

Animation class

Posted: Sat Jun 18, 2011 7:02 pm
by Elena5
Hello everyone !

I am quite new to lua and I am experiencing some difficulties with it. I am writing an animation class that looks like this :

Code: Select all

Animation = {}
Animation.__index = Animation

-- nbf : number of frame
function Animation.create(filename, x, y, width, height, nbf)
	local self = {}
	setmetatable(self,Animation)
	self.img = love.graphics.newImage(filename)
	self.img:setFilter("nearest","nearest")
	self.x = x
	self.y = y
	self.width = width
	self.height = height
	self.nbf = nbf
	self.frame = 0
	for i=0,nbf do
		self.quad[i] = love.graphics.newQuad(x + width*i, y, width, height, self.img:getWidth(), self.img:getHeight())
	end
	return self
end

function Animation:update(dt)
	local anim_speed = 10
	self.frame = (self.frame + anim_speed*dt) % self.nbf
end

function Animation:draw(x,y)
	love.graphics.drawq(self.img, self.quad[math.floor(self.frame)], x, y)
end
But I have a strange error :

Code: Select all

\animation.lua:16: attempt to index field 'quad' (a nil value)
The error is thus located here (but I can't see it !) :

Code: Select all

for i=0,nbf do
	self.quad[i] = love.graphics.newQuad(x + width*i, y, width, height, self.img:getWidth(), self.img:getHeight())
end
Could you please help me to solve this problem?

Thanks a lot !

Re: Animation class

Posted: Sat Jun 18, 2011 7:05 pm
by bartbes
When doing self.quad, you are indexing a table self.quad, but, you never created a table there. Adding a self.quad = {} before the loop should do the trick.

Re: Animation class

Posted: Sat Jun 18, 2011 7:07 pm
by Elena5
Woaw very fast answer ! I am impressed, you are totally right. That did the trick !

Thanks again !

Re: Animation class

Posted: Sun Jun 19, 2011 10:24 pm
by Ensayia
I swear this happens to me just about any time I start a small project, lol. Interested to see how your animation library works.

Re: Animation class

Posted: Mon Jun 20, 2011 3:06 pm
by Elena5
I am glad to here I am not the only one to make this kind of mistakes ! :crazy:

The animation class works with band sprite like this :
Image

Here is my animation class :

Code: Select all

Animation = {}
Animation.__index = Animation

-- nbf : number of frame
function Animation.create(filename, nbf)
	local self = {}
	setmetatable(self,Animation)
	self.img = love.graphics.newImage(filename)
	self.img:setFilter("nearest","nearest")
	local width = math.floor(self.img:getWidth() / nbf)
	local height = self.img:getHeight()
	self.nbf = nbf
	self.frame = 0
	self.quad = {}
	for i=0,nbf do
		self.quad[i] = love.graphics.newQuad(width*i, 0, width, height, self.img:getWidth(), self.img:getHeight())
	end
	return self
end

function Animation:update(dt)
	local anim_speed = 10
	self.frame = (self.frame + anim_speed*dt) % self.nbf
end

function Animation:draw(x,y)
	love.graphics.drawq(self.img, self.quad[math.floor(self.frame)], x, y)
end
Here is an example :

Code: Select all

require("animation")

function love.load()
	anm = Animation.create("img/hagane.png", 4)
end

function love.update(dt)
	anm:update(dt)
end

function love.draw()
    anm:draw(300, 300)
end
This is a very simple class, but this is all I need right now.