1
\$\begingroup\$

I'm trying to make a game with real time strategy elements, where you can build several different buildings.

The problem is that I can't seem to make the buildings stop overlapping. That is too say, if try to build while my mouse is hovering over a building, it won't build, which is good.

But the moment that my mouse is no longer over a building, I can build again. And since the buildings have a centered origin, the building is build around the coordinates where the mouse is. This causes overlap between buildings, which is bad.

I have the following code in a step event of a control object:

if (mouse_check_button_pressed(mb_left)) {
  if (!position_meeting(mouse_x, mouse_y, all)) {
      instance_create(mouse_x, mouse_y, obj_shed);
      player_money -= 100;
  }
}

Any help would be greatly appreciated.

EDIT: The code now follows the suggestions of Despicable and looks as follows:

var object, width, height;
object = obj_shed; //Change to your desired object
w = object.sprite_width;
h = object.sprite_height;

if (mouse_check_button_pressed(mb_left)) {
  if (!collision_rectangle(mouse_x-w/2,mouse_x+w/2,mouse_y-h/2,mouse_y+h/2,all,0,1)){
    instance_create(mouse_x,mouse_y,obj_shed);
    player_money -= 100;
  }
}

However, w and h give an error, so now i need to find a way how to get the width and heigh of an object that is not the calling instance (because the calling instance is a control object, in which is code is situated)

EDIT: Solution

I was able to change the solution so that the buildings won't overlap, even if the buildings aren't circular:

In the control object, in the step event, write the following:

if (mouse_check_button_pressed(mb_left)) {
      instance_create(mouse_x, mouse_y, obj_shed);
}

And in the create event of your building (shed in my case):

if (!place_meeting(mouse_x, mouse_y, all)) {
      global.player_money -= 100;
}
else {
    instance_destroy();
}

So on creation of the building, it checks if there is a collision. If so, it destroys itself. If not, it stays there and deducts the cost of the building. This works with any shape of building.

\$\endgroup\$
3
\$\begingroup\$

I'll assume that all the buildings occupy space in rectangular shape.

Instead of checking if the mouse is hovering over other buildings, you can check if the building supposed to be built collapses with any other existing buildings at the position of the mouse cursor. So this...

if (mouse_check_button_pressed(mb_left))
{
    if (!collision_rectangle(mouse_x-w/2,mouse_x+w/2,mouse_y-h/2,mouse_y+h/2,all,0,1)
    {
        instance_create(mouse_x,mouse_y,obj_shed);
        player_money -= 100;
    }
}

where w and h are the width and the height of the space building occupies, should work.

Note that you must have the mask of the building sprites set correctly, but unless you've changed something there, it should all just be fine.

Read more about the collision_rectangle() function in the documentation.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you, that already helps a lot. However, now I have the problem that I need to find w and h. This code is in the step event of a control object, so the I can't just use sprite_width and sprite_height. I tried using something like the code in the edit, but this just gives an error. \$\endgroup\$ – Twistervise Jan 12 '14 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.