TimerHandler在一个区间andengine spawing精灵 [英] TimerHandler in andengine spawing sprites at a interval

查看:110
本文介绍了TimerHandler在一个区间andengine spawing精灵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这TimerHandler在andEngine产卵在某些时候的精灵。

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

现在我已经在类级别声明的变量INT ..

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;

然后我有一个方法,让我改变速度或nextSpriteTimes。

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?

推荐答案

首先 - 在速度方法,你的日志消息的的一个错误 - 你为什么使用 Log.e 的方法?这是错误...使用 Log.d (调试)或 Log.i (信息)来代替。

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.

回到你的问题。我并没有完全明白你的意思,但我确实看到一个问题:
让我们说, nextSprite1Time = 100 addSpriteTime1 = 70 。截至直到这里,一切都很好,对不对?在五个 onTimePassed 来电,一个新的精灵将被添加。

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.

但现在你改变了 nextSprite1Time 60 addSpriteTime1 还是 70 ,并且因为它是大于60°时它会的从不的添加新的精灵!

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!

解决方案:使用> = ,而不是 == ,并没有计数器归零,但降低从他们身上 nextSpriteTime 的值,例如,对于精灵1:

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

这篇关于TimerHandler在一个区间andengine spawing精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆