Game Development Community

Mouse Pick a Single Tile in a Tile Layer.

by Jeremy Easoz · in Torque Game Builder · 06/18/2008 (6:56 pm) · 7 replies

I'm working on mouse picking a single tile in a layer.
pickTile(x,y) returns worldPos.
But it returns the same number no matter what tile in the tilelayer I pick.

Any ideas?

#1
06/19/2008 (3:34 pm)
PickTile(x,y) should / does not return the world position. It should return the position of the tile on a tile layer if used correctly. For instance, let's say you had a tile layer called $gamelayer and you trapped the mouse down position using the %worldpos return of a mouse down call back. Something like the following would return the logical tile position in the layer:

$pickedtile = $gamelayer.pickTile(%worldpos);

Picked tile should then equal something like "4 12" which would translate to the 4th tile across and the 12th tile down (from memory). If a tile does not exist at that location it should return nothing.

Post you code up so we can have a look as it makes finding these issues easier.

Hope this helps
#2
06/19/2008 (3:52 pm)
I have a 10x15 tile layer.
I opened up the console and typed echo(tileLayer.pickTile(5,5));
no matter what x and y is it returns 5 7.
#3
06/19/2008 (3:56 pm)
Prehaps im misusing it.

x/y in pick tile is world pos?
#4
06/19/2008 (4:26 pm)
Yes, its the world position. So for instance, tilelayer.pickTile(5,5) picks the tile in that layer at world position 5,5 which is close to the middle of the screen. So depending on the size of your tiles incrementing this number by a little will return the same tile. I'll try and draw something to explain:

-100,-75
 ________________________________________________________________________________________
|                 |                 |                 |                 |                 |
|                 |                 |                 |                 |                 |
|       1,1       |       1,2       |       1,3       |       1,4       |       1,5       |
|                 |                 |                 |                 |                 |
|________________ |________________ |________________ |________________ |________________ |
|                 |                 |                 |                 |                 |
|                 |                 |                 |                 |                 |
|       2,1       |       2,2       |       2,3       |       2,4       |       2,5       |
|                 |                 |                 |                 |                 |
|________________ |________________ |________________ |________________ |________________ |
|                 |                 |                 |                 |                 |
|                 |                 |                 |                 |                 |
|       3,1       |       3,2       |       3,3       |       3,4       |       3,5       |
|                 |                 |                 |                 |                 |
|________________ |________________ |________________ |________________ |________________ |
|                 |                 |                 |                 |                 |
|                 |                 |                 |                 |                 |
|       4,1       |       4,2       |       4,3       |       4,4       |       4,5       |
|                 |                 |                 |                 |                 |
|________________ |________________ |________________ |________________ |________________ |   100,75

Let's say the above is your screen and your tile layer has 5 tiles across and 4 tiles down. World position of the screen at the top left is -100, -75, the middle world position is 0, 0 and the bottom right is 100, 75. By doing some basic math we can say that each tile is roughly 40 x 37 world units. So, for instance, using the function tilelayer.pickTile(5,5) would return 3,3. Using the command tilelayer.pickTile(10,10) would still return 3,3 as we have only traveled another 5 world units to the right and down.

Does that make more sense ?

EDIT - Go Go ASCII
#5
06/19/2008 (6:01 pm)
%localx = getWord(%worldPos, 0);
%localy = getWord(%worldPos, 1);
%tile = tileMap.pickTile(%localx, %localy);

Thanks
#6
06/19/2008 (6:36 pm)
You can simplfy that by writing:

%tile = tileMap.pickTile(%worldPos);

However, where are you getting %worldPos from ? Is this part of a mouse down callback ? Try echoing the variables out at each stage and reposting. EG:

echo("World Posisiton - " %worldPos);
%tile = tileMap.pickTile(%worldPos);
echo("Tile Posisiton - " %tile)

And check the console to see what responses you are getting. Your code looks correct, it's just a matter of how you are getting your %worldPos by the looks. I'm not at a machine with TGB at the moment so I can test this.
#7
06/19/2008 (8:49 pm)
I only did that because I pass the local x/y to a function to rotate tiles.

Thanks for the help.
Good Tip