# Tile is nil

**URL:** <https://forums.solar2d.com/t/tile-is-nil/297317>\
**Category:** Lime\
**Created:** [December 14, 2011, 2:03pm UTC](https://forums.solar2d.com/t/tile-is-nil/297317 "2011-12-14T14:03:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![sismetic\_chaos](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sismetic\_chaos](https://forums.solar2d.com/u/sismetic_chaos)\
**Post date:** [December 14, 2011, 2:03pm UTC](https://forums.solar2d.com/t/tile-is-nil/297317/1 "2011-12-14T14:03:21Z")

</div>

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:

```auto
--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)  
```

I have tried to get the tile with the positions returned by functions(like worldToIsometricPosition(), IsometricToWorldPosition(), screenToGridPosition(), regular event.x and event.y, sending event as the position), but nothing so far has worked. I really appreciate any help, and it would be great if somebody could explain to me the difference between map, grid and world(as I dont know whats the difference will be between the position returned by all of these) or point to a tutorial or something of the sort [import]uid: 109816 topic\_id: 19147 reply\_id: 319147[/import]

---

<div class="post-metadata">

**Author:** ![GrahamRanson](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/g/8edcca/32.png) [@GrahamRanson](https://forums.solar2d.com/u/GrahamRanson)\
**Post date:** [December 15, 2011, 6:02am UTC](https://forums.solar2d.com/t/tile-is-nil/297317/2 "2011-12-15T06:02:47Z")

</div>

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:

```auto
  
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 ) )  
  
```

Now somewhere in your code, depending on how your game is structured, you will want code like this:

[code]

local onTap = function( event )

– First just get the screen position from the passed in table  
local screenPosition = { x = event.x, y = event.y }

– Now convert that to a world position  
local worldPosition = lime.utils.screenToWorldPosition( map, screenPosition )

– And finally convert the world position to a grid position  
local gridPosition = lime.utils.worldToGridPosition( map, worldPosition )

– Now access the top most tile at that position, you may wish to use a specific TileLayer instead of the whole map but that is up to you.  
local tile = map:getTileAt( gridPosition )

– Just to prove it works, slightly alpha out the tile  
if tile then  
tile:getVisual().alpha = 0.5  
end

end

Runtime:addEventListener( “tap”, onTap )

[/code] [import]uid: 5833 topic\_id: 19147 reply\_id: 73947[/import]

---

<div class="post-metadata">

**Author:** ![sismetic\_chaos](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sismetic\_chaos](https://forums.solar2d.com/u/sismetic_chaos)\
**Post date:** [December 15, 2011, 7:43am UTC](https://forums.solar2d.com/t/tile-is-nil/297317/3 "2011-12-15T07:43:30Z")

</div>

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 [import]uid: 109816 topic\_id: 19147 reply\_id: 73966[/import]

---

<div class="post-metadata">

**Author:** ![GrahamRanson](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/g/8edcca/32.png) [@GrahamRanson](https://forums.solar2d.com/u/GrahamRanson)\
**Post date:** [December 15, 2011, 7:56am UTC](https://forums.solar2d.com/t/tile-is-nil/297317/4 "2011-12-15T07:56:35Z")

</div>

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

```auto
\_gridPosition.row = \_gridPosition.row - 1  
\_gridPosition.column = \_gridPosition.column - 1  
```

I will update my earlier post to show how it should go.

- Edit -

Ok I can’t update my previous post so it should look like this

[code]

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 ) )

\_gridPosition.row = \_gridPosition.row - 1  
\_gridPosition.column = \_gridPosition.column - 1

[/code] [import]uid: 5833 topic\_id: 19147 reply\_id: 73968[/import]

---

<div class="post-metadata">

**Author:** ![sismetic\_chaos](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sismetic\_chaos](https://forums.solar2d.com/u/sismetic_chaos)\
**Post date:** [December 15, 2011, 7:58am UTC](https://forums.solar2d.com/t/tile-is-nil/297317/5 "2011-12-15T07:58:13Z")

</div>

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:

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

I want to change the tile for another one(grass to grass with small lake) whenever a tile is tapped. Appreciate your help [import]uid: 109816 topic\_id: 19147 reply\_id: 73969[/import]

---

<div class="post-metadata">

**Author:** ![GrahamRanson](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/g/8edcca/32.png) [@GrahamRanson](https://forums.solar2d.com/u/GrahamRanson)\
**Post date:** [December 15, 2011, 8:15am UTC](https://forums.solar2d.com/t/tile-is-nil/297317/6 "2011-12-15T08:15:04Z")

</div>

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](http://dl.dropbox.com/u/571145/globalIDs.png) [import]uid: 5833 topic\_id: 19147 reply\_id: 73971[/import]

---

<div class="post-metadata">

**Author:** ![sismetic\_chaos](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sismetic\_chaos](https://forums.solar2d.com/u/sismetic_chaos)\
**Post date:** [December 15, 2011, 8:20am UTC](https://forums.solar2d.com/t/tile-is-nil/297317/7 "2011-12-15T08:20:20Z")

</div>

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 [import]uid: 109816 topic\_id: 19147 reply\_id: 73972[/import]

---

<div class="post-metadata">

**Author:** ![GrahamRanson](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/g/8edcca/32.png) [@GrahamRanson](https://forums.solar2d.com/u/GrahamRanson)\
**Post date:** [December 15, 2011, 8:36am UTC](https://forums.solar2d.com/t/tile-is-nil/297317/8 "2011-12-15T08:36:19Z")

</div>

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:

```auto
-- Build the tile physical  
self:build()  
```

Place these:

```auto
-- Bring the new tile into focus  
self:getVisual():toFront()  
self:getVisual().isVisible = true  
```

See if that works. [import]uid: 5833 topic\_id: 19147 reply\_id: 73976[/import]

---

<div class="post-metadata">

**Author:** ![sismetic\_chaos](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sismetic\_chaos](https://forums.solar2d.com/u/sismetic_chaos)\
**Post date:** [December 15, 2011, 9:02am UTC](https://forums.solar2d.com/t/tile-is-nil/297317/9 "2011-12-15T09:02:15Z")

</div>

Yes, now it works perfectly. Thank you! You’re great :P, thank you so much [import]uid: 109816 topic\_id: 19147 reply\_id: 73980[/import]

---

<div class="post-metadata">

**Author:** ![GrahamRanson](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/g/8edcca/32.png) [@GrahamRanson](https://forums.solar2d.com/u/GrahamRanson)\
**Post date:** [December 15, 2011, 9:43am UTC](https://forums.solar2d.com/t/tile-is-nil/297317/10 "2011-12-15T09:43:36Z")

</div>

Awesome, glad it does! [import]uid: 5833 topic\_id: 19147 reply\_id: 73986[/import]

---

<div class="post-metadata">

**Author:** ![t.naz123](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@t.naz123](https://forums.solar2d.com/u/t.naz123)\
**Post date:** [July 6, 2013, 10:18am UTC](https://forums.solar2d.com/t/tile-is-nil/297317/11 "2013-07-06T10:18:21Z")

</div>

Hi, I used your code which shows the tile where the screen is touched.

But i can’t run that code, it gives me error like this

_**…\lime\lime-utils.lua:267 bad argument #1 to floor (number expected, got nill)**_

\_ **…** \_

\_ **…\lime\lime-utils.lua:267 in function ‘screenToWorldPosition’** \_

I’ve attached the snapshot of error too.

Kindly tell me why this error is happening. I’ll be very thankful to you.

---

<div class="post-metadata">

**Author:** ![t.naz123](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@t.naz123](https://forums.solar2d.com/u/t.naz123)\
**Post date:** [July 6, 2013, 10:18am UTC](https://forums.solar2d.com/t/tile-is-nil/297317/12 "2013-07-06T10:18:21Z")

</div>

Hi, I used your code which shows the tile where the screen is touched.

But i can’t run that code, it gives me error like this

_**…\lime\lime-utils.lua:267 bad argument #1 to floor (number expected, got nill)**_

\_ **…** \_

\_ **…\lime\lime-utils.lua:267 in function ‘screenToWorldPosition’** \_

I’ve attached the snapshot of error too.

Kindly tell me why this error is happening. I’ll be very thankful to you.
