andengine 中的 TimerHandler 按一定时间间隔生成精灵

发布于 2024-12-27 13:30:53 字数 1740 浏览 1 评论 0原文

我在 andEngine 中使用这个 TimerHandler 在某些时间生成精灵。

  mScene.registerUpdateHandler(new TimerHandler(0.02f, true, new ITimerCallback() {
                   @Override
                   public void onTimePassed(TimerHandler pTimerHandler) {
                          addSpriteTime1 += 2; // because timer update = 0.02 seconds
                           if (addSpriteTime1 == nextSprite1Time) {
                                  addFace();
                                   addSpriteTime1 = 0;
                           }

                           addSpriteTime2 += 2;
                           if (addSpriteTime2 == nextSprite2Time) {
                                 addFace2();
                                   addSpriteTime2 = 0;
                           }

                           addSpriteTime3 += 2;
                           if (addSpriteTime3 == nextSprite3Time) {
                                   addFace3();
                                   addSpriteTime3 = 0;
                           }
                   }
           }));

现在我在类级别声明了 int 变量。

private int nextSprite1Time = 100;// initial value, could be changed during game
private int nextSprite2Time = 100;
private int nextSprite3Time = 100;

然后我有一个方法可以让我更改速度或 nextSpriteTimes。

 private void speed(int f, int g, int h){

    this.nextSprite1Time = f;
    this.nextSprite2Time = g;
    this.nextSprite3Time = h;
    Log.e("Time Changed", String.valueOf(this.nextSprite1Time+ "," + this.nextSprite2Time + ","+ this.nextSprite3Time));

     }

问题是,例如,当我尝试改变速度时..

 speed(30, 50, 70);

它只是一起停止,现在添加了精灵,

有人看到我在哪里出错或者可以做不同的事情吗?

I am using this TimerHandler in andEngine to spawn sprites at certain times..

  mScene.registerUpdateHandler(new TimerHandler(0.02f, true, new ITimerCallback() {
                   @Override
                   public void onTimePassed(TimerHandler pTimerHandler) {
                          addSpriteTime1 += 2; // because timer update = 0.02 seconds
                           if (addSpriteTime1 == nextSprite1Time) {
                                  addFace();
                                   addSpriteTime1 = 0;
                           }

                           addSpriteTime2 += 2;
                           if (addSpriteTime2 == nextSprite2Time) {
                                 addFace2();
                                   addSpriteTime2 = 0;
                           }

                           addSpriteTime3 += 2;
                           if (addSpriteTime3 == nextSprite3Time) {
                                   addFace3();
                                   addSpriteTime3 = 0;
                           }
                   }
           }));

Now i have int variables declared at class level..

private int nextSprite1Time = 100;// initial value, could be changed during game
private int nextSprite2Time = 100;
private int nextSprite3Time = 100;

I then have a method which allows me to change the speed or the nextSpriteTimes.

 private void speed(int f, int g, int h){

    this.nextSprite1Time = f;
    this.nextSprite2Time = g;
    this.nextSprite3Time = h;
    Log.e("Time Changed", String.valueOf(this.nextSprite1Time+ "," + this.nextSprite2Time + ","+ this.nextSprite3Time));

     }

The problem is when i try to change the speed for example..

 speed(30, 50, 70);

It just stops all together and now sprites are added,

Does anyone see where i am going wrong with this or can do it differently?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…楫夜泊 2025-01-03 13:30:54

首先 - 您在 speed 方法中的日志消息不是错误 - 您为什么使用 Log.e 方法?这是针对错误的...使用 Log.d (调试)或 Log.i (信息)代替。

回到你的问题。我不太明白你的意思,但我确实看到了一个问题:
假设 nextSprite1Time = 100addSpriteTime1 = 70。到这里为止,一切都很好,对吧?在另外五个 onTimePassed 调用中,将添加一个新的精灵。

但现在您将 nextSprite1Time 更改为 60addSpriteTime1 仍然是 70,并且由于它大于 60,因此它永远不会添加新的精灵!

解决方案:使用 >= 而不是 ==,并且不要将计数器归零,而是减少其中的 nextSpriteTime 值,例如,对于精灵 1:

addSpriteTime1 += 2;
if(addSpriteTime1 >= nextSprite1Time) {
    addFace();
    addSpriteTime1 -= nextSprite1Time;
}

First of all - Your log message in speed method is not an error - why are you using Log.e method? That's for errors... Use Log.d (debug) or Log.i (information) instead.

Back to your problem. I didn't understand exactly what you meant, but I do see a problem:
Lets say that nextSprite1Time = 100 and addSpriteTime1 = 70. Up untill here, everything is fine, right? In five more onTimePassed calls, a new sprite will be added.

But now you changed nextSprite1Time to 60. addSpriteTime1 is still 70, and since it is larger than 60 it will never add a new sprite!

Solution: Use >= instead of ==, and don't zero the counters but decrease the value of nextSpriteTime from them, for example, for sprite 1:

addSpriteTime1 += 2;
if(addSpriteTime1 >= nextSprite1Time) {
    addFace();
    addSpriteTime1 -= nextSprite1Time;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文