计时器变量在GameMaker Studio 2中未倒计时。

huangapple go评论57阅读模式
英文:

Timer variable not counting down in GameMaker Studio 2

问题

我想添加一个小计时器,以便在房间之间传送时有一定的延迟,以便播放玩家的精灵动画,但由于某种原因,我的计时器变量不起作用,我不知道出了什么问题。

这是代码,在玩家与传送物体之间的碰撞事件中:

    timer = 17;
    sprite_index = spr_playerenter;
    image_speed = anim_speed;
    state = "CANT_MOVE";
    
    timer--;
    
    if (timer <= 0) {
    room_goto(other.targetRoom);
    x = other.targetX;
    y = other.targetY;
    state = "IDLE";
    }

关于计时器,我创建了变量并添加了 timer--;,因为这是需要使值减小的操作,对吧?其余部分相当简单,当计时器达到0时,玩家将被传送到变量指定的房间,位于这个 x 和 y 位置。

精灵动画确实播放了,但我想计时器不起作用,因为计时器中的任何内容都没有发生。

任何帮助都将不胜感激,谢谢!

英文:

I want to add a little timer so there's a delay when warping between rooms so a sprite animation can play for the player, but for some reason my timer variable isn't working and I don't know what's wrong with it.

Here's the code, this is in a collision event between the player and warp object

    timer = 17;
    sprite_index = spr_playerenter;
    image_speed = anim_speed;
    state = &quot;CANT_MOVE&quot;;
    
    timer--;

    if (timer &lt;= 0) {
    room_goto(other.targetRoom);
    x = other.targetX;
    y = other.targetY;
    state = &quot;IDLE&quot;;
    }

With the timer, I made the variable and added timer--; because that's what is needed to have the value decrease, right? And the rest is pretty simple, when the timer reaches 0, the player will be warped to the room stated as the variable at this x and y position.

The sprite animation did play, but I guess the timer isn't working because nothing stated in the timer happened at all.

Any help is appreciated, thanks!

答案1

得分: 1

从目前的情况来看,你正在同一个地方倒计时,这个地方也是计时器被定义的地方。

假设一切都放在Step事件中,你应该将timer = 17(可能与state = &quot;CANT_MOVE&quot;;一起)移到Create事件中。

Create事件只会在对象创建时调用一次,而Step事件会一直循环检查。这会导致计时器重置为17。这就是为什么定义的变量最好放在Create事件中的原因。

英文:

From how it currently looks like, you're counting down the timer at the same place where it's defined.

Assuming everything is currently placed in the Step Event, you should move the timer = 17 (probably along with everything until state = &quot;CANT_MOVE&quot;;) to the Create Event.

The Create Event will only be called once during creation of the object, where the Step Event is always looping and checking. That causes the timer to reset back to 17. This is why defined variables are better placed in the Create Event.

huangapple
  • 本文由 发表于 2023年7月17日 20:03:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定