Tilemap Array/Matix help.

Hi everyone,
Currently I have a tilemap and a lot of tiles which are randomly generated just to see how it performs.

--randomly populate tile ids (normally this data would be loaded from a file)  
for y = 0, map.yTiles - 1 do  
 for x = 0, map.xTiles - 1 do  
 map.tiles[#map.tiles + 1] = math.random(171)  
 end  
end   

The next step is for me to plan out each tile individually I think.

So I’m thinking I need to set up an Array or Matrix to designate the level, sort of like: {2,2,2,4,6,7…etc}
So that it reads the Array and places the appropriate tile down then checks the next number in the array and places that tile down.

Is that the right way to go about it? So I can plan the level out?
Also what exactly is it I should be using an array or matrix or something else?

Sorry & Thanks.

[import]uid: 91798 topic_id: 29201 reply_id: 329201[/import]

Ok i have a solution for everything! :smiley:

here is my code for making a tilemap

i got this from - http://pixels.ph4.se/2010/10/tilemaps-with-corona-part1/

i condensed the code a little and here is all that you need for loading and placing all the tiles

HA, no need for payed for 3rd party tools like lime, free is always better :slight_smile:

if you need any help don’t hesitate to ask.

please note:

-if your table is not big enough then errors will occur , i.e. the tilemap says 16 tiles but table has only 14

-this works with a 40x40 tiles, you can easily adjust 40’s to 32’s or other and maybe a little more work to make it adjust properly

  • this can be used for however big of a map you want (within reason)

  • this code is written in such a way that in the value’s below you put 1 less then how many tiles you want so i made 2 variables, ( tilex and tiley, each for corresponding x and y axis) and all you do is put how many tiles you want on each axis and it will do the rest for you.

  • here is my tile map example change the table values however you wish

  • if you wish to make some physics bodies then adjust the spritemap[i].currentFrame to whatever frame you want and the physics body details or other. REMEMBER that it takes 00 in the table as the first 40x40 (or whatever) and the currentframe takes the 00 as frame 01. So 01 in the table would be the 2nd image in your tilemap.png and would be frame 2.

[code]
local tilex = 18
local tiley = 14

local tilemaps =
{
{
{04,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,04},
{07,29,22,22,22,22,22,22,22,22,22,22,22,22,22,22,26,05},
{07,23,00,00,00,00,00,00,00,00,00,00,00,00,00,42,20,05},
{07,28,40,25,00,00,00,00,24,21,21,21,21,25,00,00,20,05},
{04,01,01,02,33,00,00,32,03,01,01,01,01,02,33,00,20,05},
{04,06,06,18,37,00,00,20,05,04,04,04,04,07,23,00,20,05},
{07,29,22,35,00,00,00,36,17,06,06,06,06,18,37,00,20,05},
{07,23,42,00,00,00,00,00,34,22,22,22,22,35,00,00,20,05},
{07,28,21,21,25,00,00,00,00,00,00,00,00,00,00,00,20,05},
{04,01,01,01,02,33,00,00,00,00,00,00,00,00,42,00,20,05},
{04,06,06,06,18,37,00,00,00,00,00,00,00,00,00,00,20,05},
{07,29,22,22,35,00,00,00,00,00,00,00,00,00,00,21,27,05},
{07,28,41,21,21,21,21,21,15,08,08,08,08,08,13,01,01,04},
{05,01,01,01,01,01,01,01,11,04,04,04,04,04,04,04,04,04},
},
}
tilesheet = sprite.newSpriteSheet(“tileMapImage.png”, 40, 40)
local tileset1 = sprite.newSpriteSet(tilesheet, 1, 49)
local spritemap = display.newGroup()
for y=0,tiley-1,1 do
for x=0,tilex-1,1 do
local tilesprite = sprite.newSprite( tileset1 )
tilesprite.y = y*40+20
tilesprite.x = x*40+20
spritemap:insert(tilesprite)
end
end
function loadlevel (level)
local i = 1
for y=0,tiley-1,1 do
for x=0,tilex-1,1 do
spritemap[i].currentFrame = tilemaps[level][(1+y)][(1+x)]+ 1

–if spritemap[i].currentFrame < 20 and spritemap[i].currentFrame > 1 then
–physics.addBody(spritemap[i],“static”)
–end

i = i + 1
end
end
end
loadlevel(1) [import]uid: 113909 topic_id: 29201 reply_id: 117473[/import]

Okay boxie,
You’re my hero! haha.
So I was right about working from an array. I’m currently adapting and experimenting with it.
I’ve been looking at that 160,000 tiles efficiency example and yours is a lot easier to understand.

Although culling is still needed, Corona may or may not do that for me, I’ll not take the risk and see if I can mix the two.
I’ve managed to setup a screens worth of tiles and resize them, which is useful for my brain =)

I’ve also tested out those physics bodies which are really cool =)

I think it’s time for me to experiment more and see if I can add functionality bit by bit.

Again, Thanks a bunch,
It’s really Appreciated!
Matt [import]uid: 91798 topic_id: 29201 reply_id: 117591[/import]

no problem, just trying to help someone in need, i know how it feels to be stuck on something such as this. Glad to see that it works for you, also about the culling you might be able to try somethings with it, make sure to back up your project often in case of total destructions

Your Welcome

-boxie [import]uid: 113909 topic_id: 29201 reply_id: 117614[/import]