Diagonal sprite control

T

Tonydaderp

Guest
this was a repost of a game maker steam thread:

Hi, I've been trying to work out how to recognise the correct sprite to use when using diagonals. For example if I'm holding UP then Right, my sprite will keep facing UP. Or, If I'm holding Right and then UP, my sprite continues facing Right.

I know the command will be based upon the last key pressed but I'm not sure how to implement a key registry system like that.

Thanks for the help.
I don't need movement code since I've already done that!
 

NightFrost

Member
So if I read that right, you want to preserve facing when moving into diagonals (because you don't have diagonal sprites)? You need to remember previous facing in a variable, and compare that to current facing to determine which of the four sprites to use. I wrote a piece of code a while back for that, which probably could be optimized... It preserves your sprite facing into cardinal directions when you move into diagonals.
Code:
if(XMove != 0 || YMove != 0){
    // Determine direction
    var NewDirection = 0;
    if(YMove < 0) NewDirection += 1;
    if(XMove > 0) NewDirection += 2;
    if(YMove > 0) NewDirection += 4;
    if(XMove < 0) NewDirection += 8;
    if(NewDirection != Direction) PreviousDirection = Direction;
    Direction = NewDirection;
    // Set sprite according direction and previous direction
    XScale = 1;
    if(Direction == 1){ // Up
        sprite_index = spr_walk_up;
    }
    else if(Direction == 2){ // Right
        sprite_index = spr_walk_side;
    }
    else if(Direction == 4){ // Down
        sprite_index = spr_walk_down;
    }
    else if(Direction == 8){ // Left
        sprite_index = spr_walk_side;
        XScale = -1;
    }
    else if(Direction == 3 || Direction == 6){ // Up and Right, or Down and Right
        if(PreviousDirection == 2 || PreviousDirection == 3 || PreviousDirection == 6){
            sprite_index = spr_walk_side;
        } else {
            if(Direction == 3) sprite_index = spr_walk_up;
            else sprite_index = spr_walk_down;
        }
    }
    else if(Direction == 9 || Direction == 12){ // Up and Left, or Down and Left
        if(PreviousDirection == 8 || PreviousDirection == 9 || PreviousDirection == 12){
            sprite_index = spr_walk_side;
            XScale = -1;
        } else {
            if(Direction == 9) sprite_index = spr_walk_up;
            else sprite_index = spr_walk_down;
        }
    }          
}
 
This is the best method I could come up with:

Code:
hspeed = 5 * (keyboard_check(vk_right) - keyboard_check(vk_left));
vspeed = 5 * (keyboard_check(vk_down) - keyboard_check(vk_up));

if (speed > 0) {
    state = states.walking;
    if (abs(angle_difference(old_direction,direction)) >= 90) {
        sprite_facing = floor(direction / 90);
        old_direction = sprite_facing * 90;
    } 
}
else { state = states.idle; }

sprite_index = global.player_animations[state,sprite_facing];
sprite_index is looked up out of an array using the current state and facing as the array indices. Which greatly reduces the code for selecting the sprite_index. sprites for each state are put into the array in the order: right, up, left, down.
"states" is just an enum.
 
Last edited:
T

Tonydaderp

Guest
actually, heres my code:
Code:
{
if (keyboard_check(vk_right)    && sprite_index != linkswordright  && sprite_index != linkbowright    && sprite_index != boomerright             ) {
sprite_index = linkright;
image_speed = 1;

}
if (keyboard_check(vk_right))  && place_free(x+2,y) && sprite_index != linkswordright  && sprite_index != linkbowright    && sprite_index != boomerright    {
x+=2;}










if (keyboard_check(vk_left)    && sprite_index != linkswordleft  && sprite_index != linkbowleft    && sprite_index != boomerleft            ) {
sprite_index = linkleft;
image_speed = 1;

}
if (keyboard_check(vk_left))  && place_free(x-2,y) && sprite_index != linkswordleft  && sprite_index != linkbowleft    && sprite_index != boomerleft    {
x-=2;}




if (keyboard_check(vk_up)    && sprite_index != sprite14  && sprite_index != linkbowup   && sprite_index != boomerup           ) {
sprite_index = linkup;
image_speed = 1;

}
if (keyboard_check(vk_up))  && place_free(x,y-2)  && sprite_index != sprite14  && sprite_index != linkbowup   && sprite_index != boomerup     {
y-=2;}





/////////////////////////////////

if (keyboard_check(vk_down)    && sprite_index != linksword_down  && sprite_index != linkbowdown    && sprite_index != boomerdown          ) {
sprite_index = linkdown;
image_speed = 1;

}
if (keyboard_check(vk_down))  && place_free(x,y+2) && sprite_index != linksword_down  && sprite_index != linkbowdown    && sprite_index != boomerdown     {
y+=2;}
so can you please just have a sprite for both left and right and not just mirroring right? Thanks for the help so far.
 
Top