1
\$\begingroup\$

I'm really new to the game design world so I'm sorry if this question is stupid, but here it goes! I have 3 objects, a square that the player controls, a wall that the square should collide with and be unable to pass, and a floor that the square should be unable to pass through. I have tried playing around with the Solid and the Use Physics characteristics when I create the objects but I cannot get them to collide properly. Any ideas would be appreciated. Could you please help me out?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

This method works for me:

if (keyboard_check(ord('W') && place_free(x,y-1)) {
    y -= 1;
}
if (keyboard_check(ord('A') && place_free(x-1,y)) {
    x -= 1;
}
if (keyboard_check(ord('S') && place_free(x,y+1)) {
    y += 1;
}
if (keyboard_check(ord('D') && place_free(x+1,y)) {
    x += 1;
}

As you can see this is the usual WASD movement code but this time I added place_free(x,y) which checks x and y for a collision with a solid object.

In this case I check if there will be a collision with a solid object wherever the player will be next. If there is no collision detected a.k.a "place free?" or "is this place free of any solid objects?" then the player will move. Otherwise, the player won't move because there is a collision.

place_meeting(x,y,obj) is an alternative check for objects that you choose, and do not have to be solids. If you do not want to make your Wall or Floor object a solid then you can replace place_free(x,y) with that. Like this place_meeting(x,y-1,objWall) for example.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .