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 [import]uid: 5232 topic_id: 1374 reply_id: 301374[/import]

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. [import]uid: 6678 topic_id: 1374 reply_id: 3806[/import]

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 ?
[import]uid: 7396 topic_id: 1374 reply_id: 3810[/import]

@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:

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  

LOL … I wanna write an old school shooter NOW …

Hope that makes sense… [import]uid: 6928 topic_id: 1374 reply_id: 3813[/import]

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. :slight_smile: 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 [import]uid: 5232 topic_id: 1374 reply_id: 3828[/import]

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 :slight_smile:

Somebody should write something like gridrunner or killing mutated metagalactic llamas … Jeff Minter style ftw! [import]uid: 6928 topic_id: 1374 reply_id: 3843[/import]

@Diabetic Games. Interesting, cause my version of Missile Command is close to completition. I will look forward to your game. [import]uid: 5712 topic_id: 1374 reply_id: 3851[/import]

Very helpful info. Thanks
[import]uid: 8192 topic_id: 1374 reply_id: 5260[/import]

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. [import]uid: 33640 topic_id: 1374 reply_id: 38993[/import]