首页 > 解决方案 > GMS2 中的延迟时间

问题描述

我正在努力做到这一点,这样当你点击它时,它会cursor_sprite在 0.25 秒内显示不同的内容。我目前需要一些方法来增加延迟。到目前为止,这是我的代码:

在创建事件中:

/// @description Set cursor
cursor_sprite = spr_cursor;

在步骤事件中:

/// @description If click change cursor

if mouse_check_button_pressed(mb_left)
{
    cursor_sprite = spr_cursor2;
    // I want to add the delay here.
}

标签: game-makergmlgame-maker-studio-2

解决方案


您可以为此使用内置警报,但是当它与父对象嵌套时,我不太喜欢这些。

所以不是警报,而是我会这样做的方式:

创建事件:

cursor_sprite = spr_cursor;
timer = 0;
timermax = 0.25;

我创建了 2 个变量:timer将用于倒计时,以及timermax用于重置时间。

步骤事件:

if (timer > 0)
{
    timer -= 1/room_speed //decrease in seconds
}
else
{
    cursor_sprite = spr_cursor;
}

if mouse_check_button_pressed(mb_left)
{
    cursor_sprite = spr_cursor2;
    timer = timermax;
}

对于每个计时器,我让它在 Step Event 中通过 倒计时1/room_speed,这样,它将以实时秒为单位减少值。

然后你可以通过设置定时器timer = timermax

然后,如果计时器达到零,它将在之后执行给定的操作。

虽然提醒它在 Step Event 中,所以一旦计时器到达零,else如果之前没有其他条件,它总是会到达语句。通常我使用 else 语句来改变条件,这样它就不会多次到达计时器代码。


推荐阅读