Need help with logic for randomly spawning objects and keeping track of them to remove them.

I am working on a small zombie game, I would like to spawn zombies randomly and keep track of them so if they collide with a bullet object then they are removed however if they collide with the hero they attack.

I get the logic to remove them or make them attack on collision, however I am not sure how to randomly spawn a object on screen and keep track of it.

I am not asking for anyone to write code for me, just need guidance on the task.

Thanks in advance. 

Hi @sebfloapps,

Welcome to Corona! Have you started programming this game yet? Are you using the physics engine to move objects around and detect collisions? If so, you don’t necessarily need to “track” objects, but rather just manage what happens when two objects collide, since the collision event will expose “pointers” to both objects. So, you can detect if a zombie is hit by a bullet, and then remove the bullet and kill the zombie. Or, you can detect if a zombie touches the hero, the zombie attacks. And so forth…

As for randomly spawning objects, there are many examples written by users here in the forums, so I recommend you just search for terms like “spawn” and find an example which works for you.

Best regards,

Brent

Also adding on too Brent’s questions. Will the hero be shooting the bullets? Do you want the zombies to just walk randomly or straight at the hero? By randomly I mean that they don’t know where they are going but when they are in a certain radius of the hero they start coming towards him/her.

This type of game is very simple to make. Nothing really to it. I’d be happy to code up the simple mechanics of it for you if I find the time!

–SonicX278

Thank you both for the replies. I did start coding the game. So far I have a background, hero with a shoot on tap animation but no bullet yet, just motion animation and sound triggers on tap. I also have die animation and a resting movement animation. I am using physics to detect collisions. The hero is on the left of the screen, the zombies will spawn on the right. I have an idea on how to spawn the zombies and add them to an array, I am unsure how to add a kind of global collision listener and attach it to every array object (zombie) my code generates. For walking movement for the zombies I have an animation, and will be using a transition to unless there is a better way to move them across the screen. Any tips are appreciated.

I have written in Lua before although I am not an expert, however I do write php and Javascript on a daily basis at work.

Still need help? Sorry I didn’t get back to you. I was browsing around and found this unanswered post.

you can add objects to a table with a index associated to that object to help you delete it later.

local zombie={}

to create zombies you will get something like this

local c=1

zombie[c]=new.object(zombie) – code to create a zombie

zombie.pos=c

c=c+1

to remove a zombie you can do something like this:

display.remove(zombie[zombie.pos])

 a more complete approach rearange the table where the zombie was deleted

display.remove(zombie[zombie.pos)

zombie[zombie.pos]=nil

for i=zombie.pos+1, #zombie do

  zombie[i-1]=zombie[i]

  zombie[i-1].pos=i-1

end

c=c-1

please note i didn’t test this code. it’s only an idea to get started.

This is from another question i answered. 

local physics = require("physics") physics.start() local bullet = {} local bCounter = 1 local bTimer local centerX = display.contentCenterX local centerY = display.contentCenterY local hero = display.newRect( centerX, centerY - 200, 20, 20 ) physics.addBody( hero, "dynamic" ) hero.gravityScale = 0 hero.myName = "hero" local enemy = display.newRect( centerX, centerY, 20, 20 ) physics.addBody( enemy, "static" ) enemy.gravityScale = 0 local function moveHero(event) if event.phase == "began" then display.getCurrentStage():setFocus( event.target ) elseif event.phase == "moved" then hero.x = event.x hero.y = event.y elseif event.phase == "ended" then display.getCurrentStage():setFocus(nil) end end hero:addEventListener( "touch", moveHero ) function onCollision(event) if event.phase == "began" then if event.target.myName == "hero" and event.other.myName == "bullet" then local removeIt = event.other display.remove(removeIt) end end end hero:addEventListener( "collision", onCollision ) function spawnBullet() local cirRadius = 2 local speed = 2 local xFly = enemy.x - hero.x local yFly = enemy.y - hero.y local diff = xFly \* yFly local normX = xFly / diff local normY = yFly / diff bullet[bCounter] = display.newCircle( enemy.x, enemy.y, cirRadius ) physics.addBody( bullet[bCounter], "dynamic", { isSensor = true } ) bullet[bCounter].gravityScale = 0 bullet[bCounter]:setLinearVelocity( -xFly \* speed, -yFly \* speed ) bullet[bCounter].myName = "bullet" bCounter = bCounter + 1 end bTimer = timer.performWithDelay( 500, spawnBullet, -1 )

Hi @sebfloapps,

Welcome to Corona! Have you started programming this game yet? Are you using the physics engine to move objects around and detect collisions? If so, you don’t necessarily need to “track” objects, but rather just manage what happens when two objects collide, since the collision event will expose “pointers” to both objects. So, you can detect if a zombie is hit by a bullet, and then remove the bullet and kill the zombie. Or, you can detect if a zombie touches the hero, the zombie attacks. And so forth…

As for randomly spawning objects, there are many examples written by users here in the forums, so I recommend you just search for terms like “spawn” and find an example which works for you.

Best regards,

Brent

Also adding on too Brent’s questions. Will the hero be shooting the bullets? Do you want the zombies to just walk randomly or straight at the hero? By randomly I mean that they don’t know where they are going but when they are in a certain radius of the hero they start coming towards him/her.

This type of game is very simple to make. Nothing really to it. I’d be happy to code up the simple mechanics of it for you if I find the time!

–SonicX278

Thank you both for the replies. I did start coding the game. So far I have a background, hero with a shoot on tap animation but no bullet yet, just motion animation and sound triggers on tap. I also have die animation and a resting movement animation. I am using physics to detect collisions. The hero is on the left of the screen, the zombies will spawn on the right. I have an idea on how to spawn the zombies and add them to an array, I am unsure how to add a kind of global collision listener and attach it to every array object (zombie) my code generates. For walking movement for the zombies I have an animation, and will be using a transition to unless there is a better way to move them across the screen. Any tips are appreciated.

I have written in Lua before although I am not an expert, however I do write php and Javascript on a daily basis at work.

Still need help? Sorry I didn’t get back to you. I was browsing around and found this unanswered post.

you can add objects to a table with a index associated to that object to help you delete it later.

local zombie={}

to create zombies you will get something like this

local c=1

zombie[c]=new.object(zombie) – code to create a zombie

zombie.pos=c

c=c+1

to remove a zombie you can do something like this:

display.remove(zombie[zombie.pos])

 a more complete approach rearange the table where the zombie was deleted

display.remove(zombie[zombie.pos)

zombie[zombie.pos]=nil

for i=zombie.pos+1, #zombie do

  zombie[i-1]=zombie[i]

  zombie[i-1].pos=i-1

end

c=c-1

please note i didn’t test this code. it’s only an idea to get started.

This is from another question i answered. 

local physics = require("physics") physics.start() local bullet = {} local bCounter = 1 local bTimer local centerX = display.contentCenterX local centerY = display.contentCenterY local hero = display.newRect( centerX, centerY - 200, 20, 20 ) physics.addBody( hero, "dynamic" ) hero.gravityScale = 0 hero.myName = "hero" local enemy = display.newRect( centerX, centerY, 20, 20 ) physics.addBody( enemy, "static" ) enemy.gravityScale = 0 local function moveHero(event) if event.phase == "began" then display.getCurrentStage():setFocus( event.target ) elseif event.phase == "moved" then hero.x = event.x hero.y = event.y elseif event.phase == "ended" then display.getCurrentStage():setFocus(nil) end end hero:addEventListener( "touch", moveHero ) function onCollision(event) if event.phase == "began" then if event.target.myName == "hero" and event.other.myName == "bullet" then local removeIt = event.other display.remove(removeIt) end end end hero:addEventListener( "collision", onCollision ) function spawnBullet() local cirRadius = 2 local speed = 2 local xFly = enemy.x - hero.x local yFly = enemy.y - hero.y local diff = xFly \* yFly local normX = xFly / diff local normY = yFly / diff bullet[bCounter] = display.newCircle( enemy.x, enemy.y, cirRadius ) physics.addBody( bullet[bCounter], "dynamic", { isSensor = true } ) bullet[bCounter].gravityScale = 0 bullet[bCounter]:setLinearVelocity( -xFly \* speed, -yFly \* speed ) bullet[bCounter].myName = "bullet" bCounter = bCounter + 1 end bTimer = timer.performWithDelay( 500, spawnBullet, -1 )