# removing tile on overlap with player

**URL:** <https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582>\
**Category:** Gymbyl Coding\
**Created:** [November 5, 2015, 8:22pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582 "2015-11-05T20:22:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmp909](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jmp909](https://forums.solar2d.com/u/jmp909)\
**Post date:** [November 5, 2015, 8:22pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/1 "2015-11-05T20:22:31Z")

</div>

Hi,

I’m currently porting over some Phaser prototype code to Corona

I have a ball that drops from the top of the screen and collects items on the map from “Layer1” and rebounds off objects on “CollisionLayer”.

the collectible items are drawn on a layer in Tiled so currently I’m doing this:

```
local function onCollision( event ) &nbsp; &nbsp; if ( event.phase == "began" ) then &nbsp; &nbsp; &nbsp; &nbsp; local tile = event.object1 &nbsp; &nbsp; &nbsp; &nbsp; if(tile.layerIndex==1 and not tile.collected) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(tile.gid \< 100) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tile.alpha=0 tile.collected=true &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; end end

```

my questions are

1. i’m currently using a physics layer for Layer1, so every tile is a sensor object to allow overlap check… so with a lot of tiles, I’m wondering if this is going to kill mobile performance. (in my JS prototype i just checked 8 points around the player which seemed to suffice). any other suggestions welcome! thanks

2. other than setting **tile.alpha=0** , is there a fast way to remove a tile from the map?

thanks for any advice

great library though… i got my 3 layer prototype up and running \*really\* quickly

regards

J

ps I’ve attached an image explaining the 8 way basic coordinate check “overlap” method I used in Phaser… it’s accurate enough, just not very!

---

<div class="post-metadata">

**Author:** ![Caleb\_P](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Caleb\_P](https://forums.solar2d.com/u/Caleb_P)\
**Post date:** [November 6, 2015, 11:36am UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/2 "2015-11-06T11:36:41Z")

</div>

1. A tile layer full of physical tiles usually runs pretty fast. Unless you’re experiencing a lower FPS rate, I wouldn’t worry too much about it.

On the other hand, if you’re using physics to detect which tiles you’re on, you might want to switch for other reasons. I like to stay away from Box2D as much as possible for a number of reasons - among them are complexity, “black-box-ness”, and lightweight-ness. For a simple overlap check (with maybe basic collision resolution), it’s a bit overkill.

1. To actually remove a tile (as in really, truly remove it so it won’t ever come back), you can use layer.lockTileRemoved(x, y). To only hide the tile, you can use plain ol’ alpha or isVisible setting. Keep in mind that you’ll have to reset the tile’s alpha or isVisible each time it’s culled and re-drawn.

- Caleb

---

<div class="post-metadata">

**Author:** ![jmp909](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jmp909](https://forums.solar2d.com/u/jmp909)\
**Post date:** [November 6, 2015, 12:06pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/3 "2015-11-06T12:06:08Z")

</div>

thanks Caleb,

i didn’t notice it being re-drawn originally as I have downwards gravity…but you can bounce back up. … so yes theyr’e there again

I’m having trouble accessing that function… i thought it’d be something like

```
map.layer[1].lockTileErased(tile.tileX, tile.tileY)

```

but i just get errors

thanks for any advice

j

---

<div class="post-metadata">

**Author:** ![jmp909](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jmp909](https://forums.solar2d.com/u/jmp909)\
**Post date:** [November 6, 2015, 12:11pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/4 "2015-11-06T12:11:48Z")

</div>

not sure why accessing via index didn’t work (it couldn’t find lockTileErased function)… this works great now thanks

```
&nbsp;map.layer['Layer1'].lockTileErased(tile.tileX, tile.tileY)
```

---

<div class="post-metadata">

**Author:** ![Caleb\_P](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Caleb\_P](https://forums.solar2d.com/u/Caleb_P)\
**Post date:** [November 6, 2015, 12:18pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/5 "2015-11-06T12:18:52Z")

</div>

If layer #1 isn’t a tile layer you’ll get an error because it’s not available in object layers (for obvious reasons). Otherwise, what errors are you getting?

- Caleb

---

<div class="post-metadata">

**Author:** ![jmp909](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jmp909](https://forums.solar2d.com/u/jmp909)\
**Post date:** [November 6, 2015, 12:51pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/6 "2015-11-06T12:51:38Z")

</div>

yeah i got confused somewhere

my collectibles are on ‘Layer1’… this all works now… I just wasn’t sure about the index references

```
local function onCollision( event ) &nbsp; &nbsp; if ( event.phase == "began" ) then &nbsp; &nbsp; &nbsp; &nbsp; local tile = event.object1 &nbsp; &nbsp; &nbsp; &nbsp; if(tile.layerIndex==1) then -- \<== what does layerIndex refer to (Z?) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(tile.gid \< 100 and not tile.collected) then -- these both work now, i had assumed they were bottom-up ordered &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --map.layer['Layer1'].lockTileErased(tile.tileX, tile.tileY) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.layer[3].lockTileErased(tile.tileX, tile.tileY)

```

looks like ‘Layer1’ is my third layer, but it’s layerIndex=1 because it’s on the bottom?

my “collisionshapes” objects layer also has physics enabled but its layerIndex is ‘nil’. is that because it’s not a tile layer?

(essentially ‘Layer1’ is a tile layer with all sensors, so the player can overlap and collect, &nbsp; and “collisionshapes” contains invisible objects for the platforms and bumpers)

---

<div class="post-metadata">

**Author:** ![Caleb\_P](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Caleb\_P](https://forums.solar2d.com/u/Caleb_P)\
**Post date:** [November 6, 2015, 2:29pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/7 "2015-11-06T14:29:18Z")

</div>

Hm. Layers are indexed from top to bottom; layer[1] is the top and layer[#layers] is the bottom. This made the most sense to me since it reflects the layers’ Z-indexes.  
&nbsp;

Now that I think about it, the layerIndex property does seem a little counter-intuitive. It’s meant to represent the index in Tiled, which stores the layers from back-to-front, but you’re right, it should probably be the index of the layer in map.layer. I’ll cut it into tile.tiledLayerIndex and tile.mapLayerIndex or something like that and deprecate layerIndex until I make a full switch.

[EDIT]: Also, only tiles get the layerIndex property. Sounds like layerIndex needs some serious work :).

- Caleb

---

<div class="post-metadata">

**Author:** ![jmp909](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jmp909](https://forums.solar2d.com/u/jmp909)\
**Post date:** [November 6, 2015, 2:33pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/8 "2015-11-06T14:33:41Z")

</div>

i would assume the other way round. lowest index &nbsp;= lowest z = lowest layer… i guess it depends how you treat Z.&nbsp;

if you look at the image on my last post Layer1 is the lowest layer, Layer2 is above it.

so i’d expect the bottom layer to be 1, and the layer above to be 2.

i guess that’s the opposite of how you’ve implemented it.

thanks

j

---

<div class="post-metadata">

**Author:** ![Caleb\_P](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Caleb\_P](https://forums.solar2d.com/u/Caleb_P)\
**Post date:** [November 9, 2015, 11:48am UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/9 "2015-11-09T11:48:24Z")

</div>

Weird… I’ve always thought of Z-index as going back “into” the screen with higher values. I don’t know, it just felt more intuitive to me. That’s why I made it that way in Dusk. I realize now that a lot of things use the opposite approach that you’re meaning. Not much I can do about it now - swapping the order of the layers just like that might result in some unhappy Dusk users :D.

- Caleb

---

<div class="post-metadata">

**Author:** ![jmp909](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jmp909](https://forums.solar2d.com/u/jmp909)\
**Post date:** [November 9, 2015, 12:24pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/10 "2015-11-09T12:24:05Z")

</div>

It’s easy enough to workaround now I know how it’s working, but yes normally things with higher z-order get drawn on top of others [https://eliasdaler.wordpress.com/2013/11/20/z-order-in-top-down-2d-games/](https://eliasdaler.wordpress.com/2013/11/20/z-order-in-top-down-2d-games/)

---

<div class="post-metadata">

**Author:** ![Caleb\_P](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Caleb\_P](https://forums.solar2d.com/u/Caleb_P)\
**Post date:** [November 12, 2015, 12:08pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/11 "2015-11-12T12:08:07Z")

</div>

I might change this in Dusk 1.0 (aye, Dusk isn’t even out of beta!). For the time being, you’ll have to get used to opposite Z-ordering :D.

- Caleb

---

<div class="post-metadata">

**Author:** ![Caleb\_P](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Caleb\_P](https://forums.solar2d.com/u/Caleb_P)\
**Post date:** [November 6, 2015, 11:36am UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/12 "2015-11-06T11:36:41Z")

</div>

1. A tile layer full of physical tiles usually runs pretty fast. Unless you’re experiencing a lower FPS rate, I wouldn’t worry too much about it.

On the other hand, if you’re using physics to detect which tiles you’re on, you might want to switch for other reasons. I like to stay away from Box2D as much as possible for a number of reasons - among them are complexity, “black-box-ness”, and lightweight-ness. For a simple overlap check (with maybe basic collision resolution), it’s a bit overkill.

1. To actually remove a tile (as in really, truly remove it so it won’t ever come back), you can use layer.lockTileRemoved(x, y). To only hide the tile, you can use plain ol’ alpha or isVisible setting. Keep in mind that you’ll have to reset the tile’s alpha or isVisible each time it’s culled and re-drawn.

- Caleb

---

<div class="post-metadata">

**Author:** ![jmp909](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jmp909](https://forums.solar2d.com/u/jmp909)\
**Post date:** [November 6, 2015, 12:06pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/13 "2015-11-06T12:06:08Z")

</div>

thanks Caleb,

i didn’t notice it being re-drawn originally as I have downwards gravity…but you can bounce back up. … so yes theyr’e there again

I’m having trouble accessing that function… i thought it’d be something like

```
map.layer[1].lockTileErased(tile.tileX, tile.tileY)

```

but i just get errors

thanks for any advice

j

---

<div class="post-metadata">

**Author:** ![jmp909](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jmp909](https://forums.solar2d.com/u/jmp909)\
**Post date:** [November 6, 2015, 12:11pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/14 "2015-11-06T12:11:48Z")

</div>

not sure why accessing via index didn’t work (it couldn’t find lockTileErased function)… this works great now thanks

```
&nbsp;map.layer['Layer1'].lockTileErased(tile.tileX, tile.tileY)
```

---

<div class="post-metadata">

**Author:** ![Caleb\_P](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Caleb\_P](https://forums.solar2d.com/u/Caleb_P)\
**Post date:** [November 6, 2015, 12:18pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/15 "2015-11-06T12:18:52Z")

</div>

If layer #1 isn’t a tile layer you’ll get an error because it’s not available in object layers (for obvious reasons). Otherwise, what errors are you getting?

- Caleb

---

<div class="post-metadata">

**Author:** ![jmp909](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jmp909](https://forums.solar2d.com/u/jmp909)\
**Post date:** [November 6, 2015, 12:51pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/16 "2015-11-06T12:51:38Z")

</div>

yeah i got confused somewhere

my collectibles are on ‘Layer1’… this all works now… I just wasn’t sure about the index references

```
local function onCollision( event ) &nbsp; &nbsp; if ( event.phase == "began" ) then &nbsp; &nbsp; &nbsp; &nbsp; local tile = event.object1 &nbsp; &nbsp; &nbsp; &nbsp; if(tile.layerIndex==1) then -- \<== what does layerIndex refer to (Z?) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(tile.gid \< 100 and not tile.collected) then -- these both work now, i had assumed they were bottom-up ordered &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --map.layer['Layer1'].lockTileErased(tile.tileX, tile.tileY) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.layer[3].lockTileErased(tile.tileX, tile.tileY)

```

looks like ‘Layer1’ is my third layer, but it’s layerIndex=1 because it’s on the bottom?

my “collisionshapes” objects layer also has physics enabled but its layerIndex is ‘nil’. is that because it’s not a tile layer?

(essentially ‘Layer1’ is a tile layer with all sensors, so the player can overlap and collect, &nbsp; and “collisionshapes” contains invisible objects for the platforms and bumpers)

---

<div class="post-metadata">

**Author:** ![Caleb\_P](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Caleb\_P](https://forums.solar2d.com/u/Caleb_P)\
**Post date:** [November 6, 2015, 2:29pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/17 "2015-11-06T14:29:18Z")

</div>

Hm. Layers are indexed from top to bottom; layer[1] is the top and layer[#layers] is the bottom. This made the most sense to me since it reflects the layers’ Z-indexes.  
&nbsp;

Now that I think about it, the layerIndex property does seem a little counter-intuitive. It’s meant to represent the index in Tiled, which stores the layers from back-to-front, but you’re right, it should probably be the index of the layer in map.layer. I’ll cut it into tile.tiledLayerIndex and tile.mapLayerIndex or something like that and deprecate layerIndex until I make a full switch.

[EDIT]: Also, only tiles get the layerIndex property. Sounds like layerIndex needs some serious work :).

- Caleb

---

<div class="post-metadata">

**Author:** ![jmp909](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jmp909](https://forums.solar2d.com/u/jmp909)\
**Post date:** [November 6, 2015, 2:33pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/18 "2015-11-06T14:33:41Z")

</div>

i would assume the other way round. lowest index &nbsp;= lowest z = lowest layer… i guess it depends how you treat Z.&nbsp;

if you look at the image on my last post Layer1 is the lowest layer, Layer2 is above it.

so i’d expect the bottom layer to be 1, and the layer above to be 2.

i guess that’s the opposite of how you’ve implemented it.

thanks

j

---

<div class="post-metadata">

**Author:** ![Caleb\_P](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Caleb\_P](https://forums.solar2d.com/u/Caleb_P)\
**Post date:** [November 9, 2015, 11:48am UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/19 "2015-11-09T11:48:24Z")

</div>

Weird… I’ve always thought of Z-index as going back “into” the screen with higher values. I don’t know, it just felt more intuitive to me. That’s why I made it that way in Dusk. I realize now that a lot of things use the opposite approach that you’re meaning. Not much I can do about it now - swapping the order of the layers just like that might result in some unhappy Dusk users :D.

- Caleb

---

<div class="post-metadata">

**Author:** ![jmp909](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jmp909](https://forums.solar2d.com/u/jmp909)\
**Post date:** [November 9, 2015, 12:24pm UTC](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582/20 "2015-11-09T12:24:05Z")

</div>

It’s easy enough to workaround now I know how it’s working, but yes normally things with higher z-order get drawn on top of others [https://eliasdaler.wordpress.com/2013/11/20/z-order-in-top-down-2d-games/](https://eliasdaler.wordpress.com/2013/11/20/z-order-in-top-down-2d-games/)

[Next page](https://forums.solar2d.com/t/removing-tile-on-overlap-with-player/336582.md?page=2)
