Delete overlapping objects

I have a thing where the player can place blocks on a grid. But the problem is that players can constantly keep clicking and multiple objects will be on top each other. I’ve tried checking if its colliding with itself, and then deleting that colliding object, but then it also deletes itself. Would like some help with this

Hi, just an idea without knowing what kind of game you have and how big the grid is etc.: you could have a hidden placeholder sprite with a boolean variable for each position on the grid that toggles when your player creates a block at this position. Only for one state (true or false) blocks can be created at this position.

Depending on the actual gameplay this may be even easier with the ‘cursor/touch is on an object’ - condition → only if it is not on a block, a new block can be created.

1 Like

Let’s say you’re placing an object that you named ‘brick’ when clicking on the grid.

So in your ‘create brick’ event you just need to check if the current spot is free when clicking it.
You can do that by adding the condition
‘If mouse cursor is not on object ‘brick’’, (which is the inverted condition of ‘is mouse cursor on brick’).

Then it will only place and create the new brick if there isn’t already a brick at the position you clicked!

Edit: see below for correct way to do it.

1 Like

This only works when there is one brick object on the screen. After that, it doesn’t work. This is because although you may click on a brick object, there is another brick object the cursor is not over, making the condition true.

A modification to this method is to create a boolean variable and set it to false. Then create an event with a mouse over brick condition, and action that sets the boolean variable to true. Then have a second event that creates the brick object if the boolean variable is false.

2 Likes

Edit:

After testing, yes Mr Men you were absolutely right! I thought inverting the “cursor over” condition worked like the non inverted one.

1 Like

I corrected my last reply.

for brapstu, with Mr Men’s correction, here’s a demonstration how it would work to check if a brick is not there:

2 Likes

Omg thank you guys, I never thought of using a variable to check if a block is there. It works well! Thank you all for the help

1 Like

Variables may work well there, but it is way more work than necessary and makes the code less readable. instead, you can simply put the “Cursor in on brick” condition inside a Not event instead of inverting it.

Inverting a condition checks for the contrary: instead of checking if there is a brick under the cursor, it checks if there are bricks that are not under the cursor. That is not what you want, you want the Not condition.

The not condition runs the normal condition, in this case, “is there a block under the cursor”, and then inverts the result. Therefore, it checks if there is not a block under the cursor, what you actually want to check for.

3 Likes