1
\$\begingroup\$

I am working on a simple game that contains two objects the player and the ground, and I used place_meeting to make the player walk on the ground object and it works fine but when I try to add another object to make the player walk on it, it didn't work and the game freeze, here is my code:

// vertical collision
if place_meeting(x,y+vsp,ground1) or place_meeting(x,y+vsp,ground2)
{
while ((!place_meeting(x,y+sign(vsp),ground1 )) or (!place_meeting(x,y+sign(vsp),oground1=2 )))

 y+=sign(vsp);

    if (sign(vsp) ==1)
    {
    grounded = 1;
    }

    vsp =0;

 }
else
{
grounded = 0;
}
y+= vsp;
\$\endgroup\$
  • 1
    \$\begingroup\$ Ofc the game will freeze: when you touch the ground it will never get out of the while loop. Use an if statement. \$\endgroup\$ – DH. Feb 16 '16 at 16:05
2
\$\begingroup\$

You will get into all sorts of problems with complex if statements. Here is the solution I use, and it works for any number of grounds.

  1. Define an object obj_ground. Put no code in this object, and do not define a sprite.

  2. Create your two types ground objects.

  3. For each of your ground objects, define obj_ground as their parent. This means that every time you reference obj_ground, you check both! Look up parent and child in the user manual, it is one of the tools that makes GameMaker so good.

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

I am to understand that ground1 and ground2 are your 2 types of walkable objects.

So, on line 3 of your code, in your while loop, the object in your last check is incorrect. It shoud be (!place_meeting(x,y+sign(vsp),ground2 ))

while ((!place_meeting(x,y+sign(vsp),ground1 )) or (!place_meeting(x,y+sign(vsp),ground2 )))

Try that.

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

Here's mine

//Horizontal Collision
if (place_meeting(x+hspeed,y,obj_wall))
{
    while(!place_meeting(x+sign(hspeed),y,obj_wall))
    {
        x += sign(hspeed);
    }
    hspeed = 0;
}
x += hspeed;

//Vertical Collision
if (place_meeting(x,y+vspeed,obj_wall))
{
    while(!place_meeting(x,y+sign(vspeed),obj_wall))
    {
        y += sign(vspeed);
    }
    vspeed = 0;
}
y += vspeed;
\$\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.