• 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!

Movement direction not updating.

Z

ZeroTheScyther

Guest
Hello there. I'm trying to make an platformer where this square would move on it's own to one direction then change direction when it touches an wall. Right now for some reason, it won't update when touching an wall.


///GetPlayerInput
key_right = keyboard_check(ord("D"));
key_left = -keyboard_check(ord("A"));
key_jump = keyboard_check_pressed(vk_space);

//ReactToInputs
move = 1;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;

if (place_meeting(x,y+1, Groundio))
{
vsp = key_jump * -jumpspeed
}
//HorizonatCollision
if (place_meeting(x+hsp, y, Wallio))
{
while(!place_meeting(x+sign(vsp), y, Wallio))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;

//VerticalCollision
if (place_meeting(x, y+vsp, Groundio))
{
while(!place_meeting(x, y+sign(vsp), Groundio))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;

That's the code I've got off some youtube video and it's to make the square stop when it hits an wall. That totally works but if I change it like

if (place_meeting(x+hsp, y, Wallio))
{
while(!place_meeting(x+sign(vsp), y, Wallio))
{
x += sign(hsp);
}
move = move * -1;
}
nothing happens, even tho if I add a send message, it returns that "move" changed to -1. Trying to forcefully set hsp to -4 has a weird effect and the square moves back and forth 4 pixels once it hits the wall and keeps smacking against the wall.

So, anyone can explain what should I do to make my square change direction once it collides with an wall?
 
J

Jordan Robinson

Guest
There's a mistake in your code. Line 3, you're checking vsp. It should be hsp :)
 
Z

ZeroTheScyther

Guest
There's a mistake in your code. Line 3, you're checking vsp. It should be hsp :)
Thank you for that but still no result.

Changed it to

//React
move = 1;

if (vsp < 10) vsp += grav;
hsp = move * movespeed;
if (place_meeting(x,y+1, Groundio))
{
vsp = key_jump * -jumpspeed
}
//HorizonatCollision
if (place_meeting(x+hsp, y, Wallio))
{
while(!place_meeting(x+sign(hsp), y, Wallio))
{
x += sign(hsp);
}
move = move * -1;
}
x += hsp;

//VerticalCollision
if (place_meeting(x, y+vsp, Groundio))
{
while(!place_meeting(x, y+sign(vsp), Groundio))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;
and it just goes trough the wall

If I add "hsp = move * movespeed" under the "move = move * -1;" it gets stuck agian in the wall moving 4 pixels back and forth
 
S

Snail Man

Guest
It's because you set move to 1 at the beginning of every step, so even if it gets reversed, it immediately goes back to 1 at the beginning of the next step. Just put the move=1 in your create event instead of the step
 
Z

ZeroTheScyther

Guest
It's because you set move to 1 at the beginning of every step, so even if it gets reversed, it immediately goes back to 1 at the beginning of the next step. Just put the move=1 in your create event instead of the step
Oh. I see now. Thanks a lot. So step event is a loop.
Fixed now <3
 
Top