Remove Object in Runtime

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

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

image

This depends on what your explosive box actually is. If it’s a Game Object, you can just call the 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 method on the layer - something like tile.layer.tilemapLayer.removeTileAt(tile.x, tile.y) should work. Alternatively, you can use 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.

1 Like

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

What is bombCollide() called from?

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 along with the example I posted earlier.

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

image

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.

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

image

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

image

And the function is being called here:

image

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.

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

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

If I do something like this:

image

Tell’s me that bomb3 is undefined

If I do something like this:

image

Tell’s me that bomb3 is undefined…

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