5
\$\begingroup\$

I'm looking to make a button that is activated by two of the same objects, but the code I'm using doesn't recognize the second object as a different instance. Not the best explanation but here's the code:

if place_meeting(x,y,obj_cube) && place_meeting(x,y,obj_cube)

So the game sees this as just one obj_cube and the button is pressed even if there is only one cube on the button. I've tried using variables but I might be doing it wrong.

\$\endgroup\$
  • \$\begingroup\$ You'll probably need to provide more context for the code for us to figure out what's going on. I can see immediately that your code is using a single identifier ("obj_cube") so you are in fact referring to a single cube. Why do you think that you're referring to two different objects here? \$\endgroup\$ – Gregory Avery-Weir Feb 23 '15 at 15:48
1
\$\begingroup\$

When using the place_meeting(x, y, obj) method, you can provide either an instance or object to check for as the third argument.

Your code is checking for obj_cube, and will look for all obj_cube's. What you need are variables to keep track of each individual cube.

cube1 = instance_create(x, y, obj_cube);
cube2 = instance_create(x, y, obj_cube);

Now when you use place_meeting(), you can use this:

place_meeting(x, y, cube1) && place_meeting(x, y, cube2);

This code will only check that one particular obj_cube at a time.

\$\endgroup\$
0
\$\begingroup\$

Based on this: Counting instances at position I made this script:

//script CountColliding(x,y,obj,0);
var ret;
ret = argument3;
show_debug_message(instance_place(argument0 /*x*/, argument1/*y*/,argument2 /*obj*/))
with(instance_place(argument0 /*x*/, argument1/*y*/,argument2 /*obj*/))
{
   instance_deactivate_object(id)
   ret = CountColliding(argument0,argument1,argument2,ret+1) ;
   instance_activate_object(id)
}
return ret;

Where x,y are your objects in question x and y and obj is the object to check collision with.

But I didn't like it very much so I made this one which I suppose it may be less error prone:

///CountColliding(obj)
var i,numOfColliding=0;
for (i = 0; i < instance_number(argument[0]); i += 1)
   {
   if(place_meeting(x,y,instance_find(argument[0],i)))
   numOfColliding+=1;
   }
return numOfColliding;

Which you use in your objects scope just bu passing as an argument the object that you want to check collision with.

Both return the number of colliding objects and both seem to work just fine but I don't know which one is more efficient performance wise.

Also I suppose you shouldn't use them in every step.

\$\endgroup\$
0
\$\begingroup\$

I would recommend duplicating the obj_cube or using parents. If you duplicate it the objects will do the same thing anyway.

You could then write something like this:

   if (place_meeting(x,y,obj_cube)) && if (place_meeting(x,y,obj_cube1))
\$\endgroup\$
0
\$\begingroup\$

suppose the object that runs this code is called "obj_Button", implement the following for when you want to know is two "cubes" are at the button:

if instance_position(x,y,obj_cube)!=noone //if there is some cube touching me...
with(instance_position(x,y,obj_cube)) //tell this cube to do this:
{
   if collision_line(x,y,other.x,other.y,obj_cube,false,true)!=noone //if there is another cube between me and the button
   {
      with (obj_Button)
      {
         //do the thing you want to do here.
      }
   }
}
\$\endgroup\$
-2
\$\begingroup\$

If obj_cube has rectangle mask

use below code in obj_cube step action

var inst = collision_rectangle(x,y,x+39,y+39,obj_cube,false,true);
if inst != noone{
   //your code
}
\$\endgroup\$

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.