0

我正在为学校制作一个游戏,在这个游戏中我有一个(英雄)资产,这个资产在按键上行走并在我不按键时停止。

我唯一的问题是当我停止行走时动画继续运行。换句话说,它静止不动,但它仍在行走。

这是我的代码:

/////////////////////
//     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
    }

} 
4

1 回答 1

0

这很简单,精灵将保持在行走的精灵上,因为那是您下达命令更改的最后一个精灵。
您还需要发出命令来显示站立的精灵何时停止行走。

将其放在 if-else-statement 的末尾:

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

(并将 替换spr_denpman_Loop为您站立的精灵的名称)

在旁注中,请查看您自己的代码并理解您正在阅读的内容。我知道你正在使用的演示,它只解释了程序的基础知识,我不建议将它用作游戏的基础。由于可以简化很多代码。

于 2018-01-22T15:11:21.203 回答