Page 1 of 1

Multiple Animation Instances

Posted: Thu Nov 26, 2009 12:58 pm
by Cryonics
I want to spawn/de-spawn several animated objects on screen with random interval.

Code: Select all

MyObjects = love.graphics.newAnimation(Object_img, 32, 32, 1)
And then draw, would make the animations sync.

My initial idea was that I could use something like:
Myobject .. Var = love.graphics.newAnimation(Object_img, 32, 32, 1)

It looks to me that unless LUA/LÖVE are capable of generic naming of objects, one would have to use the following:

Code: Select all

MyObject1 = love.graphics.newAnimation(Object_img, 32, 32, 1)
MyObject2 = love.graphics.newAnimation(Object_img, 32, 32, 1)
MyObject3 = love.graphics.newAnimation(Object_img, 32, 32, 1)
etc
Problem with this is that 1) limited by how many objects you predefine 2) it's not very efficient/practical
Sure if I only had to make <10 objects it would be manageable, but lets say I wanted more.

Re: Multiple Animation Instances

Posted: Thu Nov 26, 2009 1:17 pm
by Robin
For this we would use a table:

Code: Select all

MyObjects = {}
table.insert(MyObjects, love.graphics.newAnimation(Object_img, 32, 32, 1))
table.insert(MyObjects, love.graphics.newAnimation(Object_img, 32, 32, 1))
table.insert(MyObjects, love.graphics.newAnimation(Object_img, 32, 32, 1))
table.insert(MyObjects, love.graphics.newAnimation(Object_img, 32, 32, 1))
Then all animations are accessible via []:

Code: Select all

MyObjects[1] -- the first
MyObjects[2] -- the second
index = 47
MyObjects[index] -- the 47th

Re: Multiple Animation Instances

Posted: Thu Nov 26, 2009 1:24 pm
by Cryonics
Makes sense, thanks a lot :)