sprite_index不起作用Gamemaker Studio 2(GML)

时间:2018-09-18 10:30:53

标签: gml game-maker-studio-2

我有一个小的脚本,可以在按下某个键时更改精灵索引。

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

在静止不动的时候还有另一种说法可以使sprite playerSpr,因为我尝试的方式似乎引起冲突。 提前致谢 博迪

2 个答案:

答案 0 :(得分:2)

If your entity moves you can just use the speed variable to make this. For example :

  • if your entity has a positive speed (go right) you will use PlayerRightSpr

  • if your entity has a negative speed (go left) you will use PlayerLeftSpr

  • And if speed is 0 you use PlayerSpr

(Instead of using two different sprites you can use image_xscale too)

image_xscale = 1; (normal sprite)

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

And finally instead of this :

if (!key_right or key_left)

Use this :

if (!key_right || !key_left)

Or this :

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

But you are right it's not the correct way to do this

I hope this way is good

答案 1 :(得分:2)

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

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

步骤事件:

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个循环帧。

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