1

我有一个小脚本,当我按下某个键时会更改精灵索引。

 if (key_right)
    {
    sprite_index = playerRightSpr;//<-------------
    image_speed = 1;                           //|
    }                                          //|
    if (key_left)                              //|
    {                                          //|
    sprite_index = playerLeftSpr;              //|
    image_speed = 1;                           //|
    }                                          //|
    if (key_left) && (key_right)               //|
    {                                          //|
    sprite_index = playerSpr;                  //|
    image_speed = 0;                           //|
    }                                          //|
    if (!key_right or key_left) //add this and this, doesn't work
    {                           //but it does when I remove this code.
    sprite_index = playerSpr;
    image_speed = 0;
    }

有没有另一种说法,当站着不动时让精灵 playerSpr 因为我尝试的方式似乎会导致冲突。在此先感谢博迪

4

2 回答 2

2

如果您的实体移动,您只需使用速度变量即可。例如 :

  • 如果您的实体具有正速度(向右走),您将使用 PlayerRightSpr

  • 如果您的实体速度为负(向左走),您将使用 PlayerLeftSpr

  • 如果速度为 0,则使用 PlayerSpr

(您也可以使用 image_xscale,而不是使用两个不同的精灵)

image_xscale = 1; (normal sprite)

image_xscale = -1; (flip on x axis : mirror)

最后不是这个:

if (!key_right or key_left)

用这个 :

if (!key_right || !key_left)

或这个 :

if (key_right == 0 || key_left == 0)

但你是对的,这不是正确的方法

我希望这种方式很好

于 2018-09-19T08:29:15.813 回答
1

这是我的第二个解决方案。这来自我自己的项目,因此确实需要更改整个代码。

我已经用每行旁边的注释解释了每个部分。

步骤事件:

var hinput = 0; //hinput = Horizontal Input
hinput = keyboard_check(vk_right) - keyboard_check(vk_left); //the trick I've used to declare the movement system, based on the arrow keys. 
                                                             //pressing right = 1, pressing left = -1, pressing both or none = 0.

if (hinput != 0) //it is not 0, so the player is moving
{
    sprite_index = s_player_walk; //changes the current sprite to the walking animation
    image_xscale = hinput;        //changes the direction it's facing: 1 = right, -1 = left.
                                  //when it's 0, it keeps the last direction it faced.
} 
else //the player is not moving
{
    sprite_index = s_player_idle; //changes the current sprite to the idle animation
}

至于图像本身。我为此使用了 2 个单独的精灵:
s_player_idle,仅存在 1 帧,并且
s_player_walk,存在 3 个循环帧。

图像速度已在图像编辑器中为每个单独的精灵定义,因此无需在代码中再次定义。

于 2018-09-19T20:31:17.427 回答