Creating a class?

Hi,

I have a sprite animation that has collision detection, on collision detection it then plays another animated sequence and is then removed. All working fine.

Now what I want to do is to have many “instances”? of this object on screen at any one time. By instance I mean the same properties as created in the initial object.
How do I do this?, do I make a class of the object and call it in? If so could someone give me an example?

I can do this by mirroring the code over and over but this seems rather labor intensive.

Any thoughts examples greatly appreciated. [import]uid: 127675 topic_id: 30427 reply_id: 330427[/import]

You could write a function to spawn within a table, eg;

[lua]local circle = {}

local function spawn(x, y)
local circle[#circle+1] = display.newCircle(x, y, 30)
end[/lua]

Then just call spawn(x,y) as often as you like.

You could also do this within a for loop.

This is the exact same for sprites as it is for displaying a circle as done above.

Peach :slight_smile: [import]uid: 52491 topic_id: 30427 reply_id: 121951[/import]

RE-EDIT

So I can create a module and call it into the game.
I have now succeeded in doing this, my bee class is in its own module.

I can now call it into the game, success!
But I can only create one instance of the bee; I used this example and read this article:
http://developer.coronalabs.com/forum/2010/11/09/display-objects-custom-component-question#comment-10615

I actually understand this but what am I missing?

--bee.lua--  
  
module(..., package.seeall)  
   
function new()  
  
--inserted by sprite and all its functions below here--  
--main.lua--  
  
local bee = require("bee")  
   
local bees = {}  
   
local bee1 = bee.new()  
local bee2 = bee.new()  
   
bees[#bees + 1] = bee1  
bee1.name = "bee" .. #bees  
   
bees[#bees + 1] = bee2  
bee2.name = "bee" .. #bees  

This just dosnt work I am afraid, if I just

local bee = require("bee")  
   
local bees = {}  
   
local bee = bee.new()  
  

If i do the above it brings the module in and plays fine.

Any ideas? [import]uid: 127675 topic_id: 30427 reply_id: 121980[/import]

Try this and see if works:

[lua]–main.lua–

local bee = require(“bee”)

local bees = {}

for i = 1, 2 do
bees[i] = bee.new()
bees[i].name = “bee” … i
end[/lua] [import]uid: 54349 topic_id: 30427 reply_id: 122075[/import]

@leo,

thanks Leo, still no joy, I am missing something really simple here {confused} [import]uid: 127675 topic_id: 30427 reply_id: 122131[/import]

You could write a function to spawn within a table, eg;

[lua]local circle = {}

local function spawn(x, y)
local circle[#circle+1] = display.newCircle(x, y, 30)
end[/lua]

Then just call spawn(x,y) as often as you like.

You could also do this within a for loop.

This is the exact same for sprites as it is for displaying a circle as done above.

Peach :slight_smile: [import]uid: 52491 topic_id: 30427 reply_id: 121951[/import]

RE-EDIT

So I can create a module and call it into the game.
I have now succeeded in doing this, my bee class is in its own module.

I can now call it into the game, success!
But I can only create one instance of the bee; I used this example and read this article:
http://developer.coronalabs.com/forum/2010/11/09/display-objects-custom-component-question#comment-10615

I actually understand this but what am I missing?

--bee.lua--  
  
module(..., package.seeall)  
   
function new()  
  
--inserted by sprite and all its functions below here--  
--main.lua--  
  
local bee = require("bee")  
   
local bees = {}  
   
local bee1 = bee.new()  
local bee2 = bee.new()  
   
bees[#bees + 1] = bee1  
bee1.name = "bee" .. #bees  
   
bees[#bees + 1] = bee2  
bee2.name = "bee" .. #bees  

This just dosnt work I am afraid, if I just

local bee = require("bee")  
   
local bees = {}  
   
local bee = bee.new()  
  

If i do the above it brings the module in and plays fine.

Any ideas? [import]uid: 127675 topic_id: 30427 reply_id: 121980[/import]

Try this and see if works:

[lua]–main.lua–

local bee = require(“bee”)

local bees = {}

for i = 1, 2 do
bees[i] = bee.new()
bees[i].name = “bee” … i
end[/lua] [import]uid: 54349 topic_id: 30427 reply_id: 122075[/import]

@leo,

thanks Leo, still no joy, I am missing something really simple here {confused} [import]uid: 127675 topic_id: 30427 reply_id: 122131[/import]