So…
- I’ve built my own tile engine.
- It reads from a 64x64 map of data (4096 tiles)
- It renders only a 16x12x2 chunk of that, adding/removing tiles as you move. (384 tiles)
- Each row of tiles is in an imageGroup. The imageGroups are in a displayGroup.
The problem : I’m getting noticeable “chop” (OSX Sim and iPhone4) whenever I move (thus adding rows). What am I doing to cause such a huge performance hit?
The details :
- The tiles are the only element of this app at the moment. No other sprites, audio, etc.
- I use an enterFrame listener; whenever the map has travelled > x pixels in a given direction, it calls either for a new row or a new column. (So it’s not trying to create a new row every frame)
- Profiler lists the enterFrame function in red (1084ms) during a 10 second check.
**Sample Code
The enterFrame listener
local function mover(event)
if pause == false and direction ~= false then
engine.move(map, moveTable[direction][1], moveTable[direction][2])
end
end
Finding the direction is called every frame during a touch-move if…
if direction == false or ( math.abs(event.x-last.x) \> tileSize ) or ( math.abs(event.y-last.y) \> tileSize ) then
(Basically, if the difference between your last touch and this touch is great enough, check.)
The newRow function
[code]-- FUNCTION: Creates a new row of tiles based on map core data. -----------------------------------
function newRow(map, angle)
local oldRow, xPos, yPos, newY, newX
if angle == “up” then
oldRow = 1 – the local row anchor
newY = map.vis.btm[1][1].yTile-1 – the array where tile data is stored
newX = map.vis.btm[1][1].xTile
xPos = map.vis.btm[1][1].x
yPos = map.vis.btm[1][1].y - tileSize
elseif angle == “down” then
oldRow = map.vis.btm.numChildren
newY = map.vis.btm[map.vis.btm.numChildren][1].yTile+1
newX = map.vis.btm[map.vis.btm.numChildren][1].xTile
xPos = map.vis.btm[map.vis.btm.numChildren][1].x
yPos = map.vis.btm[map.vis.btm.numChildren][1].y + tileSize
end
– Create the new row group
local btm_newRow = display.newImageGroup(map.tileset.sheet) – bottom tile layer
local top_newRow = display.newImageGroup(map.tileset.sheet) – top tile layer
– Iterate to create the tiles
for i = 1, map.vis.btm[1].numChildren do
– Create the tile
local btm_tile = newTile(map, newX, newY, “btm”)
local top_tile = newTile(map, newX, newY, “top”)
– Position the tile
btm_tile:setReferencePoint(display.TopLeftReferencePoint)
btm_tile.x = xPos; btm_tile.y = yPos
top_tile:setReferencePoint(display.TopLeftReferencePoint)
top_tile.x = xPos; top_tile.y = yPos
– Increment the X tile
newX = newX + 1
– Increment the xPos
xPos = xPos + tileSize
– Insert into the imageGroup
btm_newRow:insert(btm_tile)
top_newRow:insert(top_tile)
end
– Insert the row group
if angle == “up” then
map.vis.btm:insert(1, btm_newRow)
map.vis.top:insert(1, top_newRow)
elseif angle == “down” then
map.vis.btm:insert(btm_newRow)
map.vis.top:insert(top_newRow)
end
– Clean up
oldRow, xPos, yPos, newY, newX = nil, nil, nil, nil, nil
end --/newRow() -----------------------------------------------------------------------------------[/code]
I can understand if my code could be optimized further, but I guess I’m just not seeing how it’s causing such a noticeable slowdown given how little is actually being done.
Any ideas would be appreciated!
[import]uid: 41884 topic_id: 30486 reply_id: 330486[/import]**
