GameMaker: Studio

GameMaker: Studio

View Stats:
Help with 8-Directional Movement with Sprites
I'm looking to have my object move in 8 directions but with each direction I want him to change sprites face the direction he's going.

For example he's my code:

//Movement
if keyboard_check(vk_up) && place_free(x,y-5) {y-=5}
if keyboard_check(vk_down) && place_free(x,y+5) {y+=5}
if keyboard_check(vk_right) && place_free(x+5,y-5) {x+=5}
if keyboard_check(vk_left) && place_free(x-5,y) {x-=5}

//Set Sprite
switch(direction){
case 0: sprite_index = sprite_Run_Right_; break;
case 45: sprite_index = sprite_Run_Up_Right_; break;
case 90: sprite_index = sprite_Run_Up_ break;
case 135: sprite_index = sprite_Run_Up_Left_ break;
case 180: sprite_index = sprite_Run_Left_; break;
case 225: sprite_index = sprite_Run_Down_Left_; break;
case 270: sprite_index = sprite_Run_Down_; break;
case 315: sprite_index = sprite_Run_Down_Right_l; break;
}


I've gotten him to move in the 8 directions but his sprite isn't changing while doing so. Please help.
< >
Showing 1-4 of 4 comments
The Winter Bud Sep 4, 2019 @ 11:34pm 
teh reason why your code is not getting teh correct sprite to line up is because moving an object by it's x and y doesn't update it's direction. You'll have to do that before you change your sprites.
direction=point_direction(xprevious,yprevious,x,y); //Set Sprite switch(direction){ case 0: sprite_index = sprite_Run_Right_; break; case 45: sprite_index = sprite_Run_Up_Right_; break; case 90: sprite_index = sprite_Run_Up_ break; case 135: sprite_index = sprite_Run_Up_Left_ break; case 180: sprite_index = sprite_Run_Left_; break; case 225: sprite_index = sprite_Run_Down_Left_; break; case 270: sprite_index = sprite_Run_Down_; break; case 315: sprite_index = sprite_Run_Down_Right_l; break; }
Thanks that worked.

When I release the keys (in choosen directions) What code would I use to have my player stop moving and be facing in most recent direction?
Rather than go back to the starting direction.

Zappy Sep 5, 2019 @ 9:11am 
Originally posted by jmise5:
- When I release the keys (in choosen directions) What code would I use to have my player stop moving and be facing in most recent direction? -
The same as your current sprite-switching code, just put it between...
if x!=xprevious or y!=yprevious //If the player has moved this frame {
...and a "}" (without quotes).
The Winter Bud Sep 5, 2019 @ 3:02pm 
Just in case Zappy's concept is unclear, I think this is what they mean
if (x!=xprevious or y!=yprevious) //If the player has moved this frame { direction=point_direction(xprevious,yprevious,x,y); // update direction //Set Sprite switch(direction){ case 0: sprite_index = sprite_Run_Right_; break; case 45: sprite_index = sprite_Run_Up_Right_; break; case 90: sprite_index = sprite_Run_Up_ break; case 135: sprite_index = sprite_Run_Up_Left_ break; case 180: sprite_index = sprite_Run_Left_; break; case 225: sprite_index = sprite_Run_Down_Left_; break; case 270: sprite_index = sprite_Run_Down_; break; case 315: sprite_index = sprite_Run_Down_Right_l; break; } }
Last edited by The Winter Bud; Sep 5, 2019 @ 3:03pm
< >
Showing 1-4 of 4 comments
Per page: 1530 50