【问题标题】:sprite_index not working gamemaker studio 2 (GML)sprite_index 不工作游戏制作工作室 2 (GML)
【发布时间】:2019-02-22 08:56:26
【问题描述】:

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

 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 因为我尝试的方式似乎会导致冲突。 提前致谢 菩提

【问题讨论】:

  • 看起来你的底部 if 语句缺少最后一个语句中的 !,这看起来很可能是原因。正如! 应该为每个语句声明。我还有一个不同的解决方案,我可以展示它以防万一它没有成功。
  • 这不起作用,您能告诉我其他解决方案吗?

标签: gml game-maker-studio-2


【解决方案1】:

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

  • 如果您的实体具有正速度(向右),您将使用 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)

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

我希望这种方式很好

【讨论】:

  • 感谢您的帮助。我的语法有点不对劲,但这行得通。
  • 没问题。 Game maker 第一次理解有点难,但是真的很强大
  • 按原样解决代码的工作做得很好。我想我的回答有点过度编译了。
【解决方案2】:

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

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

步骤事件:

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-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    相关资源
    最近更新 更多