更新计时器主线程的间隔
好吧,我需要知道是否有更有效的方法来解决这个问题。
我正在使用 AndEngine 将精灵放置在方法中的场景上。
现在,在整个游戏中,我更新了精灵生成的时间间隔。
我更新了整个代码中的间隔。
例如,我有一个按一定时间间隔添加精灵的计时器,我可以更改时间间隔。
private void speed(int f, int g, int h){
this.nextSprite1Time = f;
this.nextSprite2Time = g;
this.nextSprite3Time = h;
Log.d("Time Changed", String.valueOf(this.nextSprite1Time+ "," + this.nextSprite2Time + ","+ this.nextSprite3Time));
}
当运行此方法时,会发生 3 秒的延迟,然后它会冻结。
如何通过安全线程或其他方式避免这种情况?
有什么建议吗?
编辑:这是我的时间处理程序。
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 -= nextSprite1Time;
}
addSpriteTime2 += 2;
if (addSpriteTime2 >= nextSprite2Time) {
addFace2();
addSpriteTime2 -= nextSprite2Time;
}
addSpriteTime3 += 2;
if (addSpriteTime3 >= nextSprite3Time) {
addFace3();
addSpriteTime3 -= nextSprite3Time;
}
}
}));
Okay i need to know if there is a more efficient way to go about this.
I am using AndEngine to place sprites on a Scene in a method.
Now throughout the game i update the intervals that the Sprites should be spawned.
I update the intervals throughout my code.
For example i have a Timer that adds sprites at a interval, and i can change the interval
private void speed(int f, int g, int h){
this.nextSprite1Time = f;
this.nextSprite2Time = g;
this.nextSprite3Time = h;
Log.d("Time Changed", String.valueOf(this.nextSprite1Time+ "," + this.nextSprite2Time + ","+ this.nextSprite3Time));
}
When this method is ran there is a 3 second lag that takes place, and it freezes.
How can avoid this with safe threading, or anything?
Any suggestions?
EDIT: Here is my time handler.
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 -= nextSprite1Time;
}
addSpriteTime2 += 2;
if (addSpriteTime2 >= nextSprite2Time) {
addFace2();
addSpriteTime2 -= nextSprite2Time;
}
addSpriteTime3 += 2;
if (addSpriteTime3 >= nextSprite3Time) {
addFace3();
addSpriteTime3 -= nextSprite3Time;
}
}
}));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论