• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

Help with 8 Directional Movement/Collision/Sprites

J

JonahJSanders

Guest
Hello,

I am pretty new to Game Maker, and completely new to coding!

I've picked up on a few tricks from watching some videos on how to code 8 directional movement.

I have been able to get the 8 directional movement to work, and I have even been able to get a corresponding directional sprite to work for each direction, the only problem is that when I stop moving (no longer pressing any keys) the player stops, however the sprite continues moving.

I know that I need to find some way to set the sprite_index of the player back to an idle position, but I am not sure of how to do this. If at all possible, I wanted to be able to have the player be facing idle in the direction he was last walking (For example, if he was walking left, when I stop pressing the left key, I just want the player to change to an idle left sprite, the same with right, up, down, etc.)

I also need to work collision into the code as well. I know this can be accomplished with the place_free command but I cannot get it to fit into the code, it just gives me an error and I am not sure how to fix it.

So, to summarize

1)I need to make the player stop when he collides with a solid object
2)I need the player to assume an idle sprite when no keys are being pressed, preferably corresponding to the direction he was walking

I can post the code I am using right now and if anybody would be able to help me out that would be greatly appreciated!

if (keyboard_check(vk_up) && keyboard_check(vk_right))
{x+=2; y-=2; sprite_index=Nuparu_Walk_Up;}

else if (keyboard_check(vk_up) && keyboard_check(vk_left))
{x-=2; y-=2; sprite_index=Nuparu_Walk_Up;}

else if (keyboard_check(vk_down) && keyboard_check(vk_right))
{x+=2; y+=2; sprite_index=Nuparu_Walk_Down;}

else if (keyboard_check(vk_down) && keyboard_check(vk_left))
{x-=2; y+=2; sprite_index=Nuparu_Walk_Down;}

else if keyboard_check(vk_up)
{x+=0; y-=4; sprite_index=Nuparu_Walk_Up;}

else if keyboard_check(vk_left)
{x-=4; y-=0; sprite_index=Nuparu_Walk_Left;}

else if keyboard_check(vk_down)
{x+=0; y+=4; sprite_index=Nuparu_Walk_Down;}

else if keyboard_check(vk_right)
{x+=4; y-=0; sprite_index=Nuparu_Walk_Right;}
 

Xer0botXer0

Senpai
lastDirection = "" // acceptable input: n,w,s,e,nw,sw,se,ne .

so in each of those check events update lastDirection to the direction you are going.

Code:
if (keyboard_check(vk_up) && keyboard_check(vk_right))
{x+=2; y-=2; sprite_index=Nuparu_Walk_Up;lastDirection = "ne";}

else if (keyboard_check(vk_up) && keyboard_check(vk_left))
{x-=2; y-=2; sprite_index=Nuparu_Walk_Up;lastDirection = "nw";}
..etc
else if (keyboard_check(vk_down) && keyboard_check(vk_right))
{x+=2; y+=2; sprite_index=Nuparu_Walk_Down;}

else if (keyboard_check(vk_down) && keyboard_check(vk_left))
{x-=2; y+=2; sprite_index=Nuparu_Walk_Down;}

else if keyboard_check(vk_up)
{x+=0; y-=4; sprite_index=Nuparu_Walk_Up;}

else if keyboard_check(vk_left)
{x-=4; y-=0; sprite_index=Nuparu_Walk_Left;}

else if keyboard_check(vk_down)
{x+=0; y+=4; sprite_index=Nuparu_Walk_Down;}

else if keyboard_check(vk_right)
{x+=4; y-=0; sprite_index=Nuparu_Walk_Right;}

else
{
switch(lastDirection)
{
case "n":
sprite_index = Nuparu_Idle_Up;
break;

case "w":
sprite_index = Nuparu_Idle_Left;
break;

case "s":
sprite_index = Nuparu_Idle_Down;
break;


///etc
}

}
 
Last edited:
B

BlooRooster1

Guest
EDIT: replying in the wrong thread.... Can I not delete posts?
 

FoufaDjo

Member
guys can i have help pls ;c
so i finished my 8 dir movement and it works,but when we try to press 3 or 4 of the buttons like up down and right the player gose on the last pressed button
if any one knows how to fix that pls tell me
and ty ^^
 
Top