Which tile has been clicked

I’m creating an isometric grid, 18*35 elements.
First I define the grid in an array of rows and columns (different numbers for different tiles).
Then I create a display.newgroup and iterate through the array creating the tiles and popping them into the group.

The tiles have a addEventListener(“touch”, onTouch) for each one.
Using the x,y coordinates doesn’t work for detecting which tile number I’ve touched (or if it is, it’s beyond my mathematical ability) because the grid is isometric.

However when a tile is touched, I need to know either which bitmap it’s displaying OR what its position in the group is (therefore I can check the original array). Or can I add a variable to the display group with that info in it.

[import]uid: 120570 topic_id: 22966 reply_id: 322966[/import]

Put your display objects in table…could be one dimensional and give each tile an id or two dimensional so you can tell which row/column the tile that has been clicked is in.
[import]uid: 93133 topic_id: 22966 reply_id: 91737[/import]

nick is right.
Id like to add that how you handle this depends upon what you want to do in response.

the Touch event receives an event parameter.
The object that was touched is accessible using

event.target  

So if you create each object in the normal way,
on the line before you add them to the group,
do this

theNewObject.xx = x  
theNewObject.yy = y  

Now, in the Touch event, you can just ask the event.target which one it is

local x = event.target.xx  
local y = event.target.yy  

… and do stuff from there.

[import]uid: 108660 topic_id: 22966 reply_id: 91757[/import]

This is great, now I can access the information I need.
Can I change the image in the table, along the lines of

tile.image=( “patha.png”,tile.xx, tile.yy ) [import]uid: 120570 topic_id: 22966 reply_id: 91868[/import]

I’m not sure.
I have a horrible feeling you would have to unload the first tile, and create a new one in the same spot.

You might find it easier to switch to using spritesheets.
If each tile was a sprite (you dont HAVE to use physics on them), then changing the image is simply

event.target.currentFrame = 6 -- sprite number 6 from the tilesheet  

[import]uid: 108660 topic_id: 22966 reply_id: 91874[/import]

If using spritesheet, do I need to set the spritesheet for each tile as its created?

Right now this is what I do
[blockcode]
bgImageFile=“path.png”
xOffset=-36

mytile=display.newImage( bgImageFile, (x*cellWidth)+xOffset, (y*cellHeight)+yOffset )
mytile.type=level[x][kLevelRows-y]
…etc
[/blockcode]

Can I make bgImageFile equal to a sprite? (sprite.newSpriteSheet(“path.png”, 36, 60)) [import]uid: 120570 topic_id: 22966 reply_id: 91888[/import]

Have a read of this page for background info:
http://developer.anscamobile.com/reference/sprite-sheets

The following code is ‘top of the head’ stuff, so it may need a little tweak. Hopefully it setill gets the idea over.
Imagine you have 100 pictures of the same size arranged in a single picture, which is 640 x 640 pixels (each sprite being 64 x 64)

require (“sprite”)
spriteSheet = sprite.newSpriteSheet(“image.png”, 10, 10)

The number of frames in the sprite sheet is assumed to be floor(imageWidth/frameWidth) * floor(imageHeight/frameHeight). They are indexed in row major order:

1 2 3 4… 10
11 12 13…20
etc
getting a single sprite:

si = sprite.newSprite( spriteSheet )  
si.x = 200  
si.y = 200  
si.currentFrame = 6   
displayGroup:insert(si)  
--add an event handler here too  

Now the sprite is shown.
(You could do the xx, yy thing too, if you need to.)

To change the image, you use

si.currentFrame = 10

or

event.target.currentFrame = 10

====================================

But the really funky thing you get if you switch from simple tiles to sprites, is that your images can be animated.
So instead of just a patch of spikes for the unwary traveller to fall into, you could have an animated fire going on in that space, just by setting that sprite to play a set of animated images, which it does in the background for you.

[import]uid: 108660 topic_id: 22966 reply_id: 91903[/import]