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]
[import]uid: 52491 topic_id: 31423 reply_id: 125674[/import]