andengine 中的 TimerHandler 按一定时间间隔生成精灵
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先 - 您在
speed
方法中的日志消息不是错误 - 您为什么使用Log.e
方法?这是针对错误的...使用Log.d
(调试)或Log.i
(信息)代替。回到你的问题。我不太明白你的意思,但我确实看到了一个问题:
假设
nextSprite1Time = 100
和addSpriteTime1 = 70
。到这里为止,一切都很好,对吧?在另外五个onTimePassed
调用中,将添加一个新的精灵。但现在您将
nextSprite1Time
更改为60
。addSpriteTime1
仍然是70
,并且由于它大于 60,因此它永远不会添加新的精灵!解决方案:使用
>=
而不是==
,并且不要将计数器归零,而是减少其中的nextSpriteTime
值,例如,对于精灵 1:First of all - Your log message in
speed
method is not an error - why are you usingLog.e
method? That's for errors... UseLog.d
(debug) orLog.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
andaddSpriteTime1 = 70
. Up untill here, everything is fine, right? In five moreonTimePassed
calls, a new sprite will be added.But now you changed
nextSprite1Time
to60
.addSpriteTime1
is still70
, 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 ofnextSpriteTime
from them, for example, for sprite 1: