• 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

P

piksil_demon

Guest
you know in those games that have 8 directional movement, but only 4 movement animations? can you help me figure that out with this movement code here-
// variables
up = keyboard_check(ord('W'));
down = keyboard_check(ord('S'));
left = keyboard_check(ord('A'));
right = keyboard_check(ord('D'));

// movment
x+= (right - left)*spd;
y+= (down - up)*spd;

if speed > spd {
speed = spd;}

ideally it would be that the character faces the first direction pressed in the diagonal, but as long as it dosnt spazz out it will work
 

Bingdom

Googledom
Remember before, i told you to use hspeed and vspeed instead, because x+= and y+= does not get effected by variable speed.

Use this instead
hspeed = (right-left)*spd;
vspeed = (down-up)*spd;

For your answer, do this. :)
Code:
//Get Direction
dir = point_direction(0,0,hspeed,vspeed);

//Calculate which out of all the 4 angles the player is facing
face = round(dir/90);
if (face == 4) face = 0;

//Always one of these will be true
switch (face) {
    case 0:
        sprite_index = SPR_Player_Right;
        break;
    
    case 270:
        sprite_index = SPR_Player_Down;
        break;
    
    case 180:
        sprite_index = SPR_Player_Left;
        break;
    
    case 90:
        sprite_index = SPR_Player_Up;
        break;
}
 
Last edited:

Bingdom

Googledom
A lot of your questions seem like you are trying to create a RPG game, I recommend this YouTube series. ;)
 
P

piksil_demon

Guest
A lot of your questions seem like you are trying to create a RPG game, I recommend this YouTube series. ;)
two things, one. your code didnt work, and secondly- i know of that series, but i dont like how ben goes about it. the movment code he made was confusing, and the use of drag and drop, then switching to code is annoying
 
P

piksil_demon

Guest
thank you for your input, but i figured it out on my own using this code-
old_sprite_index = sprite_index;
if right { sprite_index = spr_player_right};
if left { sprite_index = spr_player_left};
if up { sprite_index = spr_player_up};
if down { sprite_index = spr_player_down};
if right and up{sprite_index=old_sprite_index};
if left and up{sprite_index=old_sprite_index};
if right and down{sprite_index=old_sprite_index};
if left and down{sprite_index=old_sprite_index};
 

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