Just for fun i want to create a game with a jet and falling enemies that you have to dodge. My problem is that i would be using the same object ( same enemy ) to spawn several times. Now, the first time the object gets spawned and it collides with the jet or the lower invisible wall it disappears as it should, but the following objects prompt me to an error message. I get a nill value. So, I guess the problem is that the second object that is created doesn’t have any fields on it and neither do the ones that follow it. I tried using tables to create the objects and it doesn’t work. I tried to spawn a new object when the previous one gets destroyed and it doesn’t work either, changing the names gave me the same result, a nil value!
local objects = {}
local objectCount = 5
for i = 1, objectCount do
local startX = math.random(display.contentWidth*0.2,display.contentWidth*0.9)
local startY = math.random(display.contentWidth*0.2,display.contentWidth*0.4)
bomb = display.newImage( “bomb1.png”, startX, -300)
bomb.myName = “bomb”
physics.addBody( bomb )
bomb.enterFrame = offscreen
end
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
elseif ( event.phase == “ended” ) then
bomb:removeSelf()
end
end
bomb.collision = onLocalCollision
bomb:addEventListener( “collision” )
jet.collision = onLocalCollision
jet:addEventListener( “collision” )
The rest of the program works fine