I have the following code and I try to remove an element from the array but it doesn’t work.
I search on the forum but without luck.
Do you guys have any idea why is not working? Is this a bug or something?
Thank you!
[code]
display.setStatusBar(display.HiddenStatusBar)
local screen_w, screen_h = display.contentWidth, display.contentHeight
local background = display.newImage(“game-bg.png”,true)
local speed = 4
background.x = screen_w / 2
background.y = screen_h / 2
local cars = display.newGroup()
function carSpawn()
local car = display.newImage(“jeep-green.png”,-100,178)
cars:insert(car)
timer.performWithDelay(math.random(500,1500),carSpawn)
print(“carSpawn()”)
end
function carDespawn(i)
cars[i]:removeSelf()
cars:remove(i)
print(“carDespawn(” … i … “)”)
end
function handlerFrame()
for i=1,cars.numChildren do
cars[i].x = cars[i].x + speed
if cars[i].x > screen_w + 50 then
carDespawn(i)
end
end
end
Runtime:addEventListener(“enterFrame”,handlerFrame)
carSpawn()
–TERMINAL ERROR:
–Runtime error
–main.lua:30: attempt to index field ‘?’ (a nil value)
[/code] [import]uid: 8241 topic_id: 14949 reply_id: 314949[/import]
[import]uid: 3826 topic_id: 14949 reply_id: 55244[/import]