[Resolved] Spawning objects from the top and collision detection

Hi. I know how to edit the code below to spawn from the top more objects at the same time, but I don’t know how to spawn them at every 500 milliseconds.
So, at first it spawns 1 object, and it prints the ballTable[i] but when I perform with delay ( the spawn function), the next objects are not differrent. I want to manipulate them individualy because I want to test collision.
Also, after I solve this problem, how I should make the collision test?

function collisionTest()  
for i = 1 , #ballTable  
if hasCollided(ballTable[i],player)  
health = health -10   

and add it to EventListener?
Thanks!

[code]
local physics = require(“physics”)
physics.start()
display.setStatusBar( display.HiddenStatusBar )
–physics.setDrawMode(“hybrid”)

–Variabile
local health = 164

local background = display.newImage(“background.jpeg”)
background.y = 210

local road = display.newImage(“road.jpg”,0,430)

local player = display.newImage(“player.png”)
player.x = display.contentWidth/2
player.y = 350

local dreapta = display.newImage(“dreapta.png”)
dreapta.x = player.x +100
dreapta.y = 455

local stanga = display.newImage(“stanga.png”)
stanga.x = player.x - 100
stanga.y = 455

local healthbar = display.newRect(143,9,health,16)
healthbar:setReferencePoint(display.TopLeftReferencePoint)
healthbar:setFillColor(38,130,224)

local health = display.newImage(“health.png”)
health:setReferencePoint(display.TopLeftReferencePoint)
health.x =135
health.y = 1

physics.addBody(road, {friction = 0.5})
road.bodyType = “static”
physics.addBody(player,{density=10.0,friction=0.2,bounce=0.3})

local function moveright (event)
player.x = player.x+5
end
dreapta:addEventListener(“touch”, moveright)

local function moveleft (event)
player.x = player.x-5
end
stanga:addEventListener(“touch”, moveleft)

local function wrapit(event)
if(player.x <32) then
player.x = 16
elseif player.x > 290 then
player.x = 305
end
end
Runtime:addEventListener(“enterFrame”, wrapit)
–Function to spawn an object
function spawn()
local ball = display.newImage(“ball.png”)
ball.x = math.random(320)
ball.y = -100
physics.addBody(ball, {bounce = 0,density= 0.1, radius = 28})
transition.to(ball, {time=2000,y=600})
return ball
end

–Create a table to hold our spawns
local ballTable = {}

–Spawn two objects
local i = 1
if i == 1 then
ballTable[i] = spawn()
end

for i = 1, #ballTable do
print(ballTable[i])
end
local spawner = timer.performWithDelay(500,spawn,1000)
[/code] [import]uid: 178587 topic_id: 31423 reply_id: 331423[/import]

I changed the last part of the code to:

--Spawn two objects  
local i = 1  
local function spawn1 ()  
for i = 1,1 do  
 ballTable[i] = spawn()  
end  
for i = 1, #ballTable do  
 print(ballTable[i])  
end  
end  
  
function collisionTest()  
 for i = 1 , #ballTable do  
 if hasCollided(ballTable[i],player) then  
 print("Collision!")  
 end  
 end  
end  
  
 local spawner = timer.performWithDelay(500,spawn1,1000)  
 Runtime:addEventListener("enterFrame", wrapit,collisionTest)  

Now,whenever a new ball is spawned, the name is printed in console simulator.
But,as you can see, I put the collision test and it doesn’t work. Any tips? [import]uid: 178587 topic_id: 31423 reply_id: 125632[/import]

Replace;
[lua] Runtime:addEventListener(“enterFrame”, wrapit,collisionTest)[/lua]

With;
[lua] Runtime:addEventListener(“enterFrame”, wrapit)
Runtime:addEventListener(“enterFrame”, collisionTest)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 31423 reply_id: 125674[/import]

Hi Peach. If I put that code I get an error on this line “if hasCollided(ballTable[i],player) then”.
And if I cut the code, and put it under spawn function, I get this error:
Runtime error assertion failed!
stack traceback:
[c]:?
[c]: in function “assert”
?: in function “getOrCreateTable”
?: in function “addEventListener”
?: in function “addEventListener”

Can you explain me please, what is wrong with my code? [import]uid: 178587 topic_id: 31423 reply_id: 125727[/import]

I changed the last part of the code to:

--Spawn two objects  
local i = 1  
local function spawn1 ()  
for i = 1,1 do  
 ballTable[i] = spawn()  
end  
for i = 1, #ballTable do  
 print(ballTable[i])  
end  
end  
  
function collisionTest()  
 for i = 1 , #ballTable do  
 if hasCollided(ballTable[i],player) then  
 print("Collision!")  
 end  
 end  
end  
  
 local spawner = timer.performWithDelay(500,spawn1,1000)  
 Runtime:addEventListener("enterFrame", wrapit,collisionTest)  

Now,whenever a new ball is spawned, the name is printed in console simulator.
But,as you can see, I put the collision test and it doesn’t work. Any tips? [import]uid: 178587 topic_id: 31423 reply_id: 125632[/import]

Replace;
[lua] Runtime:addEventListener(“enterFrame”, wrapit,collisionTest)[/lua]

With;
[lua] Runtime:addEventListener(“enterFrame”, wrapit)
Runtime:addEventListener(“enterFrame”, collisionTest)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 31423 reply_id: 125674[/import]

Hi Peach. If I put that code I get an error on this line “if hasCollided(ballTable[i],player) then”.
And if I cut the code, and put it under spawn function, I get this error:
Runtime error assertion failed!
stack traceback:
[c]:?
[c]: in function “assert”
?: in function “getOrCreateTable”
?: in function “addEventListener”
?: in function “addEventListener”

Can you explain me please, what is wrong with my code? [import]uid: 178587 topic_id: 31423 reply_id: 125727[/import]

This is not meant to be a completion to your game or anything, but you should be able to deduce your scope/functional issues from it. It does run, but needs your code (hasCollided, etc.). :slight_smile:

  
require "physics"  
  
local Random = math.random  
  
local ballTable = {}  
local player = {}  
  
physics.start()  
  
local function spawn()  
 local bcount = #ballTable + 1  
  
 ballTable[bcount] = display.newCircle(Random(320), -100, 28)  
 physics.addBody(ballTable[bcount], {bounce=0,density=0.1,radius=28})  
  
end  
  
local function wrapit(event)  
 --code  
end  
  
local function hasCollided()  
 --code  
end  
  
function collisionTest()  
 for i = 1 , #ballTable do  
 if hasCollided(ballTable[i],player)then  
 print("Collision!")  
 end  
 end  
end  
  
Runtime:addEventListener("enterFrame", wrapit)  
Runtime:addEventListener("enterFrame", collisionTest)  
  
local spawner = timer.performWithDelay(500,spawn,1000)  

I’ll check back, but try to give it a go after looking this over. [import]uid: 21331 topic_id: 31423 reply_id: 126128[/import]

You are right. I already solved the problem. I though the hasCollided function was included in Corona Library(I don’t know how).
Thank you!. [import]uid: 178587 topic_id: 31423 reply_id: 126135[/import]

Marked as resolved. Please update threads when you resolved them, otherwise people will spend time writing code that you don’t need when they might be helping others who do still need help.

Thanks!

Peach :slight_smile: [import]uid: 52491 topic_id: 31423 reply_id: 126295[/import]

This is not meant to be a completion to your game or anything, but you should be able to deduce your scope/functional issues from it. It does run, but needs your code (hasCollided, etc.). :slight_smile:

  
require "physics"  
  
local Random = math.random  
  
local ballTable = {}  
local player = {}  
  
physics.start()  
  
local function spawn()  
 local bcount = #ballTable + 1  
  
 ballTable[bcount] = display.newCircle(Random(320), -100, 28)  
 physics.addBody(ballTable[bcount], {bounce=0,density=0.1,radius=28})  
  
end  
  
local function wrapit(event)  
 --code  
end  
  
local function hasCollided()  
 --code  
end  
  
function collisionTest()  
 for i = 1 , #ballTable do  
 if hasCollided(ballTable[i],player)then  
 print("Collision!")  
 end  
 end  
end  
  
Runtime:addEventListener("enterFrame", wrapit)  
Runtime:addEventListener("enterFrame", collisionTest)  
  
local spawner = timer.performWithDelay(500,spawn,1000)  

I’ll check back, but try to give it a go after looking this over. [import]uid: 21331 topic_id: 31423 reply_id: 126128[/import]

You are right. I already solved the problem. I though the hasCollided function was included in Corona Library(I don’t know how).
Thank you!. [import]uid: 178587 topic_id: 31423 reply_id: 126135[/import]

Marked as resolved. Please update threads when you resolved them, otherwise people will spend time writing code that you don’t need when they might be helping others who do still need help.

Thanks!

Peach :slight_smile: [import]uid: 52491 topic_id: 31423 reply_id: 126295[/import]