Search code examples
game-maker-languagegame-maker-studio-2

Object won’t change sprites


I’m working on a 2D Platformer Game in GMS 2 and my player object won’t change sprites when I push the buttons that are supposed to make him change to his shooting and crouching poses. I have tried putting the different poses into different sprites, and putting the poses into different frames in the same sprite. Neither of these solutions has worked and I am kind of desperate for some help with this.

Currently the poses are in separate sprites and the code looks like this:

// Movement
key_right = keyboard_check(ord(“D”))
key_left = keyboard_check(ord(“A”))
key_jump = keyboard_check(ord(“W”))
key_crouch = keyboard_check(ord(“S”)) // This button doesn’t work

// Attacks
key_shoot = keyboard_check(vk_space) // Neither does this one
key_punch = keyboard_check(ord(“J”))
key_kick = keyboard_check(ord(“K”))

...

if (!key_left) and (!key_right) {
    sprite_index = spr_player_stand
    image_speed = 0
    image_index = 0
} else if (key_left) and (key_right) {
    sprite_index = spr_player_stand
    image_speed = 0
    image_index = 0
} else if (key_left) {
    xscale = -0.6
    sprite_index = spr_player_run
} else if (key_right) {
    xscale = 0.6
    sprite_index = spr_player_run
} else if (key_crouch) {
    sprite_index = spr_player_crouch
} else if (key_shoot) {
    sprite_index = spr_player_shoot
} else if (key_punch) {
    sprite_index = spr_player_punch
} else if (key_kick) {
    sprite_index = spr_player_kick
}

P.S. Please don’t bother correcting my lack of “;” at the end of the lines. I know that it is a bad habit and I know that I am doing it so don’t bother telling me to stop it.


Solution

  • First: This is a Programmer forum, that is why everyone will suggest you to use semicolons ";" in the end of each statement;

    You acctually code of else if statement work with only one way, meaning that only one of all those block will be executed in one script execution.

    So if the first block level is executed the others are totally ignored.

    So when key_crouch is true in the else if system it never reach the key_crouch block because first it found that key_left and key_right is false;

    so we need to analyze how make this system reliable to be able to all block to being executed then needed whitout being affected by the others blocks.

    Try with this one:

    if (key_left) and (key_right) {
        sprite_index = spr_player_stand;
        image_speed = 0;
        image_index = 0;
    } else if (key_left) {
        xscale = -0.6;
        sprite_index = spr_player_run;
    } else if (key_right) {
        xscale = 0.6;
        sprite_index = spr_player_run;
    } else if (key_crouch) {
        sprite_index = spr_player_crouch;
    } else if (key_shoot) {
        sprite_index = spr_player_shoot;
    } else if (key_punch) {
        sprite_index = spr_player_punch;
    } else if (key_kick) {
        sprite_index = spr_player_kick;
    } else {
        sprite_index = spr_player_stand;
        image_speed = 0;
        image_index = 0;
    }
    

    i Just did the following:

    Take out the first block when it check it key_left and key_right is negative and then put it the end of that the system like a default statement of a switch system.