Hello all,
I’m looking for a little basic advice about tile engines. I’ll first state that I have purchased Lime, and it’s nice, but might not be quite right for my current project, so I’m investigating a custom engine.
I’ve created a simple 2D array (table) for each layer in my map. Some layers are for visual elements, some for properties, etc. There can be multiple visual layers scrolling at different speeds for a parallax feel. I’ve implemented it with ImageSheets and ImageGroups. Here’s some simple code to demonstrate my algorithm:
[lua] local iv = {} – instance variables
– Set up the Image Sheet with the tile set
local options =
{
width = 64,
height = 64,
numFrames = 16,
sheetContentWidth = 256,
sheetContentHeight = 256
}
iv.tiles = graphics.newImageSheet( “images/tiles.png”, options )
– 2D table to hold a single layer of the map
iv.map = {}
iv.map[1] = { 1, 0, 0, 1, 0, 1, 1, 1, 0, 0 }
iv.map[2] = { 4, 7, 0, 10,5, 2, 1, 1, 0, 0 }
iv.map[3] = { 0, 1, 0, 4, 5, 9, 9, 2, 0, 0 }
iv.map[4] = { 0, 1, 0, 0, 0, 4, 6, 11, 0, 0 }
iv.map[5] = { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 }
iv.map[6] = { 0, 4, 5, 5, 5, 5, 7, 0, 0, 0 }
iv.map[7] = { 0, 0, 0, 12,0, 0, 1, 0, 0, 0 }
iv.map[8] = { 0, 12,0, 0, 0, 0, 10,5, 7, 0 }
iv.map[9] = { 5, 5, 3, 5, 7, 0, 1, 0, 1, 0 }
iv.map[10] = { 0, 0, 1, 0, 1, 0, 10,5, 2, 0 }
– Set up the Image Group with the current tile set
iv.map_group = display.newImageGroup( iv.tiles )
– The first layer in the map
iv.layer1 = {}
– Walk the table and add each tile to its spot in the layer (happens once per layer in setup)
local x, y
local i = 1
for y=1,#iv.map do
for x=1,#iv.map[1] do
local tile_number = iv.map[y][x]
if tile_number ~= 0 then
iv.layer1[i] = display.newImageRect( iv.tiles, tile_number, 64, 64 )
iv.layer1[i]:setReferencePoint( display.TopRightReferencePoint )
iv.layer1[i].x, iv.layer1[i].y = (x-1)*64, (y-1)*64
i = i + 1
end
end
end
– Moves each tile in a layer, one at a time, a delta in the x and y directions
local function move_all( layer, dx, dy )
local i
for i=1,#iv.layer do
iv.layer[i].x = iv.layer[i].x + dx
iv.layer[i].y = iv.layer[i].y - dy
end
end
– Sample move. This would actually be in listeners to move based on user input
move_all( layer1, 100, 100 )[/lua]
So my question is, does this look like the proper way to move a set of tiles around? It seems bizarre to me, because I am looping over every tile in the layer and moving it individually. I would have expected some sort of function to move an entire display group, but I didn’t see one. Can anyone tell me if I’m just missing something like that, or if anyone sees anything else that looks like I’m just crazy? I can see this getting a bit out of hand when maps get larger, say 256x256.
I would also like a way to hide all tiles at once, like group:setVisible(false) and then just turn on the tiles that are in the visible area to help with rendering speed. I’ll deal with this once I see how the performance is. Maybe Corona does this automatically with offscreen culling and it would realize not to draw the parts of the group that are off the screen?
Thanks all!
Nate
[import]uid: 118346 topic_id: 34999 reply_id: 334999[/import]
[import]uid: 41884 topic_id: 34999 reply_id: 139225[/import]