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…
) [import]uid: 18622 topic_id: 29219 reply_id: 329219[/import]