Проблемы с перемещением GameMaker studio 2

Я делаю игру для школы, в этой игре у меня есть актив (герой), этот актив ходит по нажатию клавиш и останавливается, когда я не нажимаю клавишу.

Единственная проблема, с которой я сталкиваюсь, это когда анимация «Я перестаю ходить» продолжает работать. Другими словами, он стоит на месте, но все же ходит.

Это мой код:

/////////////////////
//     MOVING      //
/////////////////////

if(keyboard_check(vk_left)){                // moving left collisions
    dir=-1;                                 // set the correct direction
    image_xscale = dir;                     // make the sprite face the correct direction
                    // if we are not jumping or falling


    sprite_index = spr_denpman_Loop;                // set the sprite to walking


    x=x-xspeed                              // move the player left
    c2 = -1;
    c3 = -1;
    // check the points at the bottom of the sprite
    c1 = tilemap_get_at_pixel(obj_Spel.map,x-(sprite_get_width(sprite_index)/2),y-1);               // left
    c3 = tilemap_get_at_pixel(obj_Spel.map,x,y-1);                                                  // center
    if( y&$3f>0 ) c2=tilemap_get_at_pixel(obj_Spel.map,x-(sprite_get_width(sprite_index)/2),y+1);   // left below (only check if there is a tile below)
    if(c1 == 3) || (c2 == 3){                                                                   // if we are intersecting with a box
        x = real(x&$ffffffc0)+(sprite_get_width(sprite_index)/2);                               // stop the player from moving
    }

    if(x < 0){                              // the the player has moved off the edge of the screen
        x = room_width;                     // wrap around to the other side of the screen
    }
}else if(keyboard_check(vk_right)){         // moving right collisions (check with else so that both directions cant be triggered at the same time)
    dir=1;                                  // set the correct direction
    image_xscale = dir;                     // make the sprte face the correct direction 
                    // if we are not jumping or falling
    sprite_index = spr_denpman_Loop;                // set the sprite to walking
    image_speed = anim_speed;   
    x=x+xspeed;                             // move the player right
    c2 = -1;
    c3 = -1;
    // check the points at the bottom of the sprite
    c1 = tilemap_get_at_pixel(obj_Spel.map,x+(sprite_get_width(sprite_index)/2),y-1);               // right
    c3 = tilemap_get_at_pixel(obj_Spel.map,x,y-1);                                                  // center
    if( y&$3f>0 ) c2=tilemap_get_at_pixel(obj_Spel.map,x+(sprite_get_width(sprite_index)/2),y+1);   // right below (only check if there is a tile below)
    if(c1 == 3) || (c2 == 3){                                                                   // if we are intersecting with a box
            x = real(x&$ffffffc0)+obj_Spel.tilesize-(sprite_get_width(sprite_index)/2);         // stop the player from moving
    }

    if(x > room_width){                     // the the player has moved off the edge of the screen
        x = 0;                              // wrap around to the other side of the screen
    }

} 

person markm    schedule 22.01.2018    source источник
comment
Эта функция пытается сделать слишком много вещей на слишком низком уровне. Разделите его на значимые блоки и проанализируйте по частям.   -  person Bartek Banachewicz    schedule 22.01.2018


Ответы (1)


Это довольно просто, спрайт останется в ходячем спрайте, потому что это последний спрайт, в который вы дали команду измениться.
Вам также нужно сделать команду, чтобы показать стоящий спрайт, когда он перестает ходить.

Поместите это в конец вашего оператора if-else:

else 
{
    sprite_index = spr_denpman_Loop;  //stops the player if there's no key pressed.
}

(И замените spr_denpman_Loop на имя вашего постоянного спрайта)

И кстати, пожалуйста, посмотрите на свой собственный код и поймите, что вы читаете. Я знаю демоверсию, которую вы используете, и она объясняет только самые основы программы, я не могу рекомендовать использовать ее в качестве основы для игры. Так как много кода можно упростить.

person Steven    schedule 22.01.2018