Animation class

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
Elena5
Prole
Posts: 21
Joined: Sat Jun 18, 2011 6:53 pm

Animation class

Post 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 !
Last edited by Elena5 on Sat Jun 18, 2011 7:05 pm, edited 1 time in total.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Animation class

Post 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.
Elena5
Prole
Posts: 21
Joined: Sat Jun 18, 2011 6:53 pm

Re: Animation class

Post by Elena5 »

Woaw very fast answer ! I am impressed, you are totally right. That did the trick !

Thanks again !
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: Animation class

Post 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.
Elena5
Prole
Posts: 21
Joined: Sat Jun 18, 2011 6:53 pm

Re: Animation class

Post 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.
Post Reply

Who is online

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