Hey guys,
I try to build a simple sidescroller cargame.
I got a table with all loaded elements (hills), and when I got past them by 6000 pixels, they should be removed. So this works good at first, but after a few times, every object gets removed. The strange thing is, that its still displayed.
on every 60 frames I try to delete things like this:
function deleteOldHill()
print(“Delete? Objects:”… #loaded_elements)
for i = #loaded_elements, 1, -1 do
if(loaded_elements[i] ~= nil) then
print ("i: " …i… “, car.x:”…carShape.x … ", thisHill.x: ", loaded_elements[i].x ) end
if (loaded_elements[i] ~= nil and loaded_elements[i].x ~= nil and carShape.x - loaded_elements[i].x > 6000) then
loaded_elements[i]:removeSelf()
loaded_elements[i] = nil
free_hills = free_hills+1
print(“removing!”)
end
end
end
So for creating hills I got this one:
function createHill()
local j = math.random (4)
local imageOutline = graphics.newOutline( 5, “img/hills/hill”…j…".png")
local newHill = display.newImageRect(world, “img/hills/hill”…j…".png", hillw, hillh )
newHill.anchorX = 0
newHill.anchorY = 0
newHill.x = spawnX + left_margin
newHill.y = spawnY + top_margin
physics.addBody( newHill, “static”, { outline=imageOutline, bounce=0.3, friction=1 } )
--table.insert(loaded_elements, newHill)
loaded_elements[#loaded_elements+1] = newHill
end
Which works pretty good.
I also set a global variable free_hills, which counts the number of hills missing, which should be generated on next frame.
function checkHills()
--creating hills
if(free_hills>0) then
createHill()
free_hills = free_hills-1
end
--deleting hills
deleteOldHill()
end
This function gets called every second.
So, the problem is, that after a while, the loaded_elements table is empty. There are no other places where this table gets modified.
Output while playing:
Delete? Objects:3
09:15:49.396 i: 3, car.x:7457.470703125, hill.x: 8384
09:15:49.396 i: 2, car.x:7457.470703125, hill.x: 4384
09:15:49.396 i: 1, car.x:7457.470703125, hill.x: 384
09:15:49.396 removing!
09:15:50.446 Delete? Objects:4
09:15:50.446 i: 4, car.x:8671.560546875, hill.x: 12384
09:15:50.446 i: 3, car.x:8671.560546875, hill.x: 8384
09:15:50.446 i: 2, car.x:8671.560546875, hill.x: 4384
09:15:51.502 Delete? Objects:4
09:15:51.502 i: 4, car.x:9855.939453125, hill.x: 12384
09:15:51.502 i: 3, car.x:9855.939453125, hill.x: 8384
09:15:51.502 i: 2, car.x:9855.939453125, hill.x: 4384
09:15:52.536 Delete? Objects:4
09:15:52.536 i: 4, car.x:11027.685546875, hill.x: 12384
09:15:52.536 i: 3, car.x:11027.685546875, hill.x: 8384
09:15:52.536 i: 2, car.x:11027.685546875, hill.x: 4384
09:15:52.536 removing!
09:15:53.674 Delete? Objects:0
09:15:54.897 Delete? Objects:0
09:15:56.007 Delete? Objects:0
09:15:57.047 Delete? Objects:0
09:15:58.082 Delete? Objects:0
I don’t get lost immediately, I can still drive, but after the hill I’m on theres no new one to generate.
Can someone help me? Why does this problem occur? Should I try an entirely different approach?
Thank you in advance
PS: I attatched the source (level1.lua is important)