which tile is touched

hi,
I have a tile map and if the user touched on a tile i want to know what type of tile it is.
So i tried to make an eventlistener for every tile, but this isn’t working.
spritemap.t=spritemap[i].currentFrame is the information that i want (tile type)

my code:

[lua]local tilemaps =
{
{
{ 13, 14, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, },
{ 13, 15, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 13, 13, 15, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 16, 13, 8, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 13, 14, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 8, 9, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 1, 3, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 12, 16, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 11, 9, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, },
},
{
{ 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 6, 0, 6, 0, 0, 0, 0, },
{ 0, 4, 4, 4, 0, 5, 5, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 4, 4, 5, 4, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 5, 5, 4, 5, 5, 5, },
{ 0, 0, 0, 0, 0, 0, 0, 0, },
{ 5, 5, 5, 5, 4, 4, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, },
{ 5, 5, 5, 5, 5, 5, 5, 5, },
}
}
– Load our tileset, each tile is 128x128 pixels
– Our PNG is 768x384 and thus have capacity of 18 tiles
tilesheet = sprite.newSpriteSheet(“tiles.png”, 128, 128)
local tileset1 = sprite.newSpriteSet(tilesheet, 1, 18)

– Create a display group
– Put one sprite in it for each tile
local spritemap = display.newGroup()
for y=0,11,1 do
for x=0,15,1 do
local tilesprite = sprite.newSprite( tileset1 )
tilesprite.x = x*128+64
tilesprite.y = y*128+64

tilesprite.xx = x
tilesprite.yy = y

tilesprite.id=x,y;
print(tilesprite);
–tilesprite:addEventListener(“tap”, tapped);
spritemap:insert(tilesprite)
end
end
function loadlevel (level)
local i = 1
for y=0,11,1 do
for x=0,15,1 do

– Determine which frame of the tileset to use for this sprite
– As specified by our tilemap
spritemap[i].currentFrame = tilemaps[level][(1+y)][(1+x)]+1

print(i)
print(x)
print(spritemap[i].currentFrame);

spritemap.xx=x
spritemap.yy=y
spritemap.t=spritemap[i].currentFrame

print(spritemap.xx)
print(spritemap.yy)
print(spritemap.t)

tilelistener = “tile” … i
print(tilelistener)
tilelistener:addEventListener(“tap”, go);

i = i + 1

end
end
end[/lua]

[edit]
which is obvious because there isn’t really an object (image) linked to the id number of the tile. I added the code that select the sprite for each tile (i still can’t get it right… :frowning: ) [import]uid: 18622 topic_id: 29219 reply_id: 329219[/import]

. [import]uid: 18622 topic_id: 29219 reply_id: 117737[/import]

. [import]uid: 18622 topic_id: 29219 reply_id: 117690[/import]

Well, lots of code in there I don’t understand, but if you want to find a tile you should write a function that works like this:

  1. As input it needs event.x/event.y (where you’re touching) and the size of tiles (I’m assuming they’re square)

  2. Use simple math to find out your tile location. (ie: tileX = math.floor(event.x/tileSize) ) That should give you the row/column variables to look up in your table.

  3. You should probably store important information on each tile as you make it, since it can be difficult to look up filenames. ie:

var = 6 local tile = display.newImage(type[var]) tile.type = var [import]uid: 41884 topic_id: 29219 reply_id: 118496[/import]