# Remove Object in Runtime

**URL:** <https://phaser.discourse.group/t/remove-object-in-runtime/2095>\
**Category:** Phaser 3\
**Created:** [April 13, 2019, 12:16pm UTC](https://phaser.discourse.group/t/remove-object-in-runtime/2095 "2019-04-13T12:16:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Walker](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@Walker](https://phaser.discourse.group/u/Walker)\
**Post date:** [April 13, 2019, 12:16pm UTC](https://phaser.discourse.group/t/remove-object-in-runtime/2095/1 "2019-04-13T12:16:12Z")

</div>

I would like to remove an object (which is a tile layer) from my scene whenever my character collides with that object. In the case it is an explosive box, where, whenever the character collides with the box, it will lose life, an explosion animation occurs and the box would be removed from the map. Any tips?

![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/4/4370df9605c48408b78cc988b1ec383ea2f9fe9d.png)

I already have a function for this, but I’m missing the part of removing the box from the scene.

![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/3/3dfdb61bac34db336f51a5f6acc4ff01d0ea3445.png)

---

<div class="post-metadata">

**Author:** ![Telinc1](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/telinc1/32/60_2.png) [@Telinc1](https://phaser.discourse.group/u/Telinc1)\
**Post date:** [April 13, 2019, 12:35pm UTC](https://phaser.discourse.group/t/remove-object-in-runtime/2095/2 "2019-04-13T12:35:02Z")

</div>

This depends on what your explosive box actually is. If it’s a Game Object, you can just call the [`destroy`](https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.GameObject.html#destroy) method on it to remove it from the Scene. If it’s a tile on a Dynamic Tilemap Layer, you can use the [`removeTileAt`](https://photonstorm.github.io/phaser3-docs/Phaser.Tilemaps.DynamicTilemapLayer.html#removeTileAt) method on the layer - something like `tile.layer.tilemapLayer.removeTileAt(tile.x, tile.y)` should work. Alternatively, you can use [`putTileAt`](https://photonstorm.github.io/phaser3-docs/Phaser.Tilemaps.DynamicTilemapLayer.html#putTileAt) to change the tile.

As for getting a reference to the explosive box, how is your `bombCollide` method being called? If it’s from a Collider (Arcade Physics), you’ll get the two colliding objects as arguments.

---

<div class="post-metadata">

**Author:** ![Walker](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@Walker](https://phaser.discourse.group/u/Walker)\
**Post date:** [April 13, 2019, 2:45pm UTC](https://phaser.discourse.group/t/remove-object-in-runtime/2095/3 "2019-04-13T14:45:44Z")

</div>

I saw the example you gave me, but I could not figure out how to apply it properly in my case. What I want to remove is effectively a Tile layer, because that’s how I defined it when I created the map in the Tiled editor. With this in mind, this box has the following properties in my game:

![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/d/dc6a84c5cbe793139072ceb264f2f5af63130670.png)  
 ![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/1/1515be721ba8008a6420a1fbb3f41e7b7e315325.png)  
 ![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/b/beb086aa04e4382d3a0374f056d34a9684c520d7.png)  
Can you give me an example using the information I provide? I’m new to this Game and Tiled stuff, I’m still not familiar with some procedures

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [April 13, 2019, 5:52pm UTC](https://phaser.discourse.group/t/remove-object-in-runtime/2095/4 "2019-04-13T17:52:07Z")

</div>

What is `bombCollide()` called from?

---

<div class="post-metadata">

**Author:** ![Telinc1](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/telinc1/32/60_2.png) [@Telinc1](https://phaser.discourse.group/u/Telinc1)\
**Post date:** [April 13, 2019, 5:56pm UTC](https://phaser.discourse.group/t/remove-object-in-runtime/2095/5 "2019-04-13T17:56:49Z")

</div>

Tile layers are Game Objects, so you can just `destroy` them. If you store `bomba3` on your Scene as `this.bomba3`, you should be able to use `this.bomba3.destroy()` from your collision handler. Still, it would be helpful to see how you actually add the collision handler, especially if this is not the only bomb in the level.

That said, I’m not sure whether using one tile layer for each bomb you need is a good solution. It’s not efficient and it’d make it quite annoying to add more than one bomb. It would be easier, in the long run, to put all bombs on one dynamic layer and use [tile callbacks](https://photonstorm.github.io/phaser3-docs/Phaser.Tilemaps.DynamicTilemapLayer.html#setTileIndexCallback) along with the example I posted earlier.

---

<div class="post-metadata">

**Author:** ![Walker](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@Walker](https://phaser.discourse.group/u/Walker)\
**Post date:** [April 13, 2019, 6:07pm UTC](https://phaser.discourse.group/t/remove-object-in-runtime/2095/6 "2019-04-13T18:07:07Z")

</div>

I forgot to show how I was calling the function, my bad. Here it is:

![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/d/d2cfbc7353e996a62bba6f0ffe23934590fb2c27.png)

Whenever there is a collision with the particular element, this is how the game should respond. Btw was incorrect in that a tile layer was a Game Objects. You were right, haha.

PS: There is only one bomb on the map.

---

<div class="post-metadata">

**Author:** ![Walker](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@Walker](https://phaser.discourse.group/u/Walker)\
**Post date:** [April 13, 2019, 6:15pm UTC](https://phaser.discourse.group/t/remove-object-in-runtime/2095/7 "2019-04-13T18:15:56Z")

</div>

Given your response, I went to test to see if it would be possible to execute correctly. The code is as follows:

![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/4/429755c295d75f3e8cc2940a2ba36a7fae4a0d6f.png)

But it turns out that in execution there arises an error which says that such property is indefinite:

![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/d/d3e6c4aaa999c9690ba13562c15dd9f9b3ee486f.png)

And the function is being called here:

![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/d/dbb958caf436c75fcb53d6e9e556052c23423ee9.png)

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [April 14, 2019, 1:41am UTC](https://phaser.discourse.group/t/remove-object-in-runtime/2095/8 "2019-04-14T01:41:10Z")

</div>

You need to use the `tile` argument (2nd position) that will be passed to your `collideBomb()` function.

See the `hitCoin()` function in [game objects/tilemap/collision/tile callbacks](https://labs.phaser.io/edit.html?src=src%5Cgame%20objects%5Ctilemap%5Ccollision%5Ctile%20callbacks.js).

---

<div class="post-metadata">

**Author:** ![Walker](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@Walker](https://phaser.discourse.group/u/Walker)\
**Post date:** [April 14, 2019, 3:23pm UTC](https://phaser.discourse.group/t/remove-object-in-runtime/2095/9 "2019-04-14T15:23:50Z")

</div>

I noticed the explanation more or less, but I was seeing the code that you made available to me and in your case you are using dynamicLayer while I am using staticLayer. In the function you told me to see, you are making the call using coinLayer which is your dynamic layer. In my case would I have to make the call using (next image)?

![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/3/31d90e49edcf5032d83f38f6077d089079944831.png)

It is for the variable bomba3 that I declare that it is a staticLayer

If I do something like this:

![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/d/d767ad762a77a71a110d1bec6f767610d522feba.png)

Tell’s me that bomb3 is undefined

If I do something like this:

![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/8/8b930e1ba4baa57eb81637386fc947917ead4fcb.png)

Tell’s me that bomb3 is undefined…

I’m not really realizing how to solve this problem …
