Creating objects in tables

Hi all,

I'm reaching out right now to gain a little more understanding of LUA and Corona SDK in terms of the creation of "objects" within a program. At the current point in time, I am creating a game which involves various ships flying across the screen while the player tries to dodge and shoot them. So far, it is functional (not very efficient though :P).

My one problem is understanding how to create many objects of the same type (you'll have to forgive me, I normally program in Object-Orientated languages). For instance if I want a ship with properties and its own functions, how could I create more than one instance of these. Currently, I can only have one of each object on the screen as allowing more on would mean that when one is destroyed, so would the rest. I currently use a table which removes objects that are placed within it each game frame,

1
2
3
4
for i = 1, #toRemove do
        toRemove[i].parent:remove(toRemove[i])
        toRemove[i] = nil
end

You would do something like this,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
display.setStatusBar(display.HiddenStatusBar)
 
math.randomseed(os.time())
 
local function printName (event)
        print (event.target.name)
end
 
local ship = {}
 
for i = 1, 10 do
        ranX, ranY = math.random(20,300), math.random(20,460)
        ship[i] = display.newCircle( ranX, ranY, 25 )
        ship[i].name = "ship"..i..""
        ship[i]:addEventListener("tap", printName)
end
views:1490 update:2011/12/28 9:26:54
corona forums © 2003-2011