Hey
I am having a few problems within the enterFrame (game loop) function that I have. Unfortunately, I have a lot happening in there and I am struggling to find ways in which I can optimise this.
My actual problem is that I have a for loop that will constantly iterate over the group of display objects that are “alive” and will check the object’s x and y values within the screen to determine whether or not that object is within a certain grid threshold or not, if so, it assigns that thing to a grid coord. So, imagine that a grid exists and each point in that grid contains the following:
x1 = coordinates of first x value in grid
x2 = coordinates of second x value in grid
y1 = coordinates of first y value in grid
y2 = coordinates of second y value in grid
The code:
[lua]
local function setGridCoords( thing )
for x=1, noGridCellsX do
– y Coord is set every down transition so there’s no need to iterate over the y coords in the grid
local y = thing.yCoord
local x1 = gridArr[x][y].x1
local x2 = gridArr[x][y].x2
local y1 = gridArr[x][y].y1
local y2 = gridArr[x][y].y2
if ( thing.isAlive == true and ( thing.x >= x1 ) and ( thing.x < x2 )
and ( thing.y >= y1 ) and ( thing.y < y2 ) ) then
thing.xCoord = x
thing.yCoord = y
return
end
end
end
local function gameLoop( event )
for i=thingGroup.numChildren,1,-1 do
local thing = thingGroup[i]
setGridCoords( thing )
end
end
function scene:enterScene( event )
Runtime:addEventListener(“enterFrame”, gameLoop)
end
[/lua]
I basically, want to know if there’s any simple ways that I can optimise this. These “things” are constantly on the move - they transition across the screen from left to right, and down the screen if they collide with anything (each other, walls, objects, etc…).
I’ve thought about trying to restrict the number of times that the for loop and setGridCoords() are called within the game method - because they really don’t need to be called 30/60 times per second, but i didn’t want to introduce any modulus functions in there that might slow the game down further. The frame rate really starts to take a hit once I start spawning a lot of these things.
Any suggestions on what to try would be very welcome. Please also bare in mind that the above code has been drawn up from memory (as I am at work and my boss is right over my shoulder!), so if I miss anything then I can try my best to fill the gaps 
Cheers
Rich