Sample code for dynamically creating, managing, and removing lots of bullets/projectiles in a game?

Hi all,

I'm new around these parts so please bear with me!

Does anyone have some sample code of how to handle a lot of objects that have the same qualities but are handled independently? e.g. bullets or projectiles in a game?

I'd like to be able to check for collisions against targets and remove the bullets when they're off screen without affecting the other bullets.

Does anyone have some sample code I could take a look at or could perhaps point me to the right part of the documentation? I'm looking to create a lot of bullets dynamically. Thanks a lot -

Nick

I think the simplest and most efficient way to do this, at least what I would do, is just store a reference to each bullet image and its properties in a table. On enterFrame, you would loop through each item in the table, move the image, check for collisions, etc.

Just take a look at Lua tables. They are really simple to use and would definitely be a good option for something like this.

May I just jump in and add another relevant Q to this thread ? What's the best system for sprites within Corona seeing as it's all the rage now to 'spawn' a sprite as needed, destroying it when it's no longer needed.
I've never been overly comfortable with this approach, coming as I do from the 'olden days' of computing when memory management was crucial. Even on development programs that give you Spawn/Destroy commands I tend to use a table of off-screen sprites that I recycle. Which of the two systems would be kinder to Corona on iPhone or Android, or is there minimal difference ?

@delta .. I believe Corona caches stuff anyway. Having it "destroyed" does not mean it is free'd anyway.

@DiabeticGames .. You may just use one "display.newGroup()" for all bullets and iterate through them. Similar to deleting items in the group you should iterate backwards over this group so if you remove one the indexing does not get wrong. The "display.newGroup()" has "numElements" instead of "#obj" for getting the count. The remaining stuff can be accessed like an table (array).

I am a big fan of using "newGroup()" for creating groups and hierarchy.

And remember: You can extend any object with you own info .. just by adding it...

Some "freeform" example should go like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
bullets=display.newGroup()
 
-- add a bullet (or multiple)
 
local bullet=display.newImage("bullet.png")
bullet.x=123
bullet.y=123
bullet.fadecounter=66 -- 2 seconds lifetime
bullet.damage_type='elemental' -- hehe
bullet.move_x=0.5 // move distance per frame
bullet.move_y=-1.2 // move distance per frame
 
bullets:insert(bullet)
 
--- later (called from enterFrame Event Handler) ---
 
-- calculate how many "frames" happened since last call
 
frames = (system.getTimer() - lasttimer) / 33
 
-- iterating all the "bullets"
 
for n=bullets.numElements,1,-1 do
 local b=bullets[n]
 
 -- move it
 b.x=b.x+frames*b.move_x
 b.y=b.y+frames*b.move_y
 
 b.fadecounter=b.fadecounter-frames
 
 if b.fadecounter<=0 or (b.x>=320 and b.x<=0) or (b.y>=480 or b.y<=0) then
    b:removeSelf() -- bullet is gone...
    -- or: b.parent:remove(b)
    -- or: bullets:remove(b)
    -- or: bullets:remove(n)
 elsif hittest(b,t) then
    if b.damage_type == t.armor_type
       t.health=t.health-100
    else
       t.health=t.health-500
    end
    if t.health <= 0 then
       t:explode()
    end
    b:removeSelf() -- bullet is gone..
 end
end

DFox and OderWat - thanks, I was trying to create a for loop to run through the table of bullets to check for them being off screen to remove them dynamically but couldn't quite nail the syntax (I'm coming from ActionScript) - this is super helpful. Thanks!

I'll try it out and post back to let you know how it goes.

OderWat - You'll be happy to hear I'm working on a simplistic 'Missile Command' clone for my first project. :) I'd work on an old school shooter (Gradius is my fav) but I don't know how well they'd lend themselves to a touch screen!

Nick

I do not enjoy to play those games actually... It is more that I like to program some less complex stuff than in my job in between :)

Somebody should write something like gridrunner or killing mutated metagalactic llamas .... Jeff Minter style ftw!

@Diabetic Games. Interesting, cause my version of Missile Command is close to completition. I will look forward to your game.

Very helpful info. Thanks

I can't get it to work. I think the code is right, and I have the "bullet.png" in the folder.
But it still keeps showing error messages.

views:1457 update:2011/9/30 9:15:39
corona forums © 2003-2011