Tile is nil

Im new to Corona, Lime, Lua and even mobile applications :P, and dont understand very well how to make some things to work.

I would like some help, because Im trying to get a tile object out of screen x and y positions(event.x, event.y) in an isometric map, whenever someone clicks on a tile. The problem is the tile always returns nil(I have tried to get the positions right, but nothing I have done works, so I would like some help

I thank you before hand, for taking the time to read this and helping if you can.

Here's the last code I tried to make this work:

1
2
3
4
5
6
7
8
--Converting screen position to world...
local screenToWorld = lime.utils.screenToWorldPosition(map, event)
--...then to isometric
local isoPosition = lime.utils.worldToIsometricPosition(map, screenToWorld)
        
--tile now is nil
--returned isoPosition.x would be something like 2, then just a little to the right would be -5.5
local tile = grassLayer:getTileAt(isoPosition,false)

Hey, welcome to the world of mobile development!

Firstly I will explain the different coordinate systems.

Screen Position - This is, as you have most likely worked out, the exact position something is on the current screen relative the the screen origin.

World Position - This is the exact pixel position of something relative to the maps origin.

Grid Position - This is the row and column number of a tile/object relative to the maps origin.

Secondly, you will need to add a little bit of code into "lime-utils.lua", find the function "worldToGridPosition" and you will see an empty block after a check for the "isometric" orientation, simply add this code to it:

1
2
3
4
5
local xScale, yScale = _map:getScale()
                
_gridPosition.column = floor( _position.y / ( _map.tileheight * yScale ) + _position.x / ( _map.tilewidth * xScale ) )
_gridPosition.row = floor( _position.y / ( _map.tileheight * yScale ) - _position.x / ( _map.tilewidth * xScale ) )
                

Great man, seriously thank you! It works perfectly, I get the tile, just a detail, when I tap on a tile, it highlights not that tile, but the tile diagonal to it(not the tapped tile), is that normal?

Thank you, seriously :P, and for the quick response

Whoops my bad, in the first block of code you also need to add these two lines of code:

1
2
_gridPosition.row = _gridPosition.row - 1
_gridPosition.column = _gridPosition.column - 1

Yeah, thanks, It works perfectly right now. One quick question if I dont take much of your time, how do I know what's the gid to put on setImage().
Im working with the IsometricAndPlayer example(with the map and the tileset), I tried this:

1
2
3
4
if tile then
  tile:setImage(0)
  --Also tried setting the gid to 1 and 2
end

The GID is controlled by your Tilesets in Tiled.

Essentially each individual tile in a tileset has an ID, these start from 1 and then go sequentially up from left to right, top to bottom. With me so far?

Then the GID ( global id ) is the tiles' ID but each tilesets first id is 1 more than the last id of the previous tileset.

http://dl.dropbox.com/u/571145/globalIDs.png

Ok, I think I understand, but right now I only have 1 tileset(with 3 tiles), so the first tile should be GID == 1, right? But whenever I tap on the tile(with tile:setImage(1)) it only highlights(or alpha) the tile not changing the tile.

Thanks for taking your time helping me out

Yup that is correct, ID 1.

If you go into "lime-tile.lua", find the function Tile:setImage() and scroll to the bottom if it, just after these lines:

1
2
-- Build the tile physical
self:build()

Yes, now it works perfectly. Thank you! You're great :P, thank you so much

Awesome, glad it does!

views:1578 update:2012/1/1 13:29:50
corona forums © 2003-2011