• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker My sprite pierces object[SOLVED BY MYSELF]

G

GalGames

Guest
upload_2017-8-11_0-1-52.png

My sprite keeps with its way when it should stop because there are blocks with collision codes
Why I get this error? (CODE BELOW)

Code:
/// @description Desplazamiento

//Desplazamiento horizontal
var keyLeft, keyRight;

keyLeft = keyboard_check(vk_left);
keyRight = keyboard_check(vk_right);

//Velocidad horizontal

if (keyLeft) Vx = -1;
else if (keyRight) Vx = 1;

x += Vx;

//Colisión Horizontal
repeat(abs(Vx))
{
    if (!place_meeting(x + Vx,y,obj_block))
    {
        x += Vx;
    }
    else
    {
        Vx = 0;
        break;
    }
}
Vx = 0
 
N

Nornocci

Guest
Hi there GalGames,

I'm assuming this code takes place in the step event of your character object. Correct me if I'm wrong! The first thing I would note is that you're essentially using repeat() as an if statement. I would start by reworking it into:
Code:
if(!place_meeting(x+Vx,y,obj_block)&&Vx!=0)
{
    x += Vx;
}
That should do everything your original repeat statement did. The && operator is useful for checking two conditions at a time. If both are true, then (statement1)&&(statement2) is true.

That said, the fix probably lies in this code:
Code:
x+=Vx;
It is outside of the collision check statement, and this means that your character will move if the left or right key is pressed, regardless of the presence of a block in the way. Remove that, and your character should stop walking through walls.

Hope this helped :)

- Nornocci
 
G

GalGames

Guest
Hi there GalGames,

I'm assuming this code takes place in the step event of your character object. Correct me if I'm wrong! The first thing I would note is that you're essentially using repeat() as an if statement. I would start by reworking it into:
Code:
if(!place_meeting(x+Vx,y,obj_block)&&Vx!=0)
{
    x += Vx;
}
That should do everything your original repeat statement did. The && operator is useful for checking two conditions at a time. If both are true, then (statement1)&&(statement2) is true.

That said, the fix probably lies in this code:
Code:
x+=Vx;
It is outside of the collision check statement, and this means that your character will move if the left or right key is pressed, regardless of the presence of a block in the way. Remove that, and your character should stop walking through walls.

Hope this helped :)

- Nornocci
This was very helpful. Thanks for take your time to explain me the codes and it worth! Thanks very much :)
 
Top