Hey guys, I’ve never had this issue before, but for some reason either my for loop or something else is just malfunctioning in my code. Can anybody try this code?
-
Basically a bunch of tiles are created, on top of the other.
-
Tiles are moved down by their y property through table iteration.
-
When they reach the near bottom of the screen they are removed and nilled
-
Tiles that have not reached the bottom should keep going down.
Problem is it working fine, but once I reach the fourth row they all stop. It seems like an error in the way it’s looping through the table. I tried many variations of the for loop to no avail. Any help would be appreciated.
Here’s the code:
tileTable = {} local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 function createTileMap(horizontalRowLength, verticalRowLength, tileSize) for i = 1, horizontalRowLength do color1 = math.random(0, 1) tile\_Vertical = display.newRect(0, 0, tileSize, tileSize) tile\_Vertical.x = (i - .5) \* tileSize tile\_Vertical.y = screenH - (tileSize\*.5) tile\_Vertical:setFillColor( color1, color1, color1 ) --Fill rows table table.insert(tileTable, tile\_Vertical) for b = 1, verticalRowLength do color1 = math.random(0, 1) tile\_Horizontal = display.newRect(0, 0, tileSize, tileSize) tile\_Horizontal.x = tile\_Vertical.x tile\_Horizontal.y = tile\_Vertical.y - (tileSize \* b) tile\_Horizontal:setFillColor( color1, color1, color1 ) --Fill rows table table.insert(tileTable, tile\_Horizontal) end end end horizontalRowLength = 7 tileSize = 30 verticalRowLength = 6 createTileMap(horizontalRowLength, verticalRowLength, tileSize) function moveTiles() for i = #tileTable, 1, -1 do tile = tileTable[i] if tile ~= nil then tile.y = tile.y + 5 if tile.y \> screenH then tileTable[i]:removeSelf() tileTable[i] = nil end end end end Runtime:addEventListener("enterFrame", moveTiles)