我需要帮助解决一个涉及TimerHandlers的奇怪问题。出于某种原因,当我在我的定时器组上调用unregisterUpdateHandler时,它们不会在第一次传递时被删除。这个问题让我疯狂,因为它对我来说完全没有意义。我只添加了一次定时器,并在重新启动我的关卡时取消注册。如果我没有注册一组新的计时器,旧的计时器会持续存在,好像我从未取消注册它们一样,直到我再次重新启动这个级别然后它们终于消失了。还有其他人遇到过这个问题吗?
这些都属于同一类
@Override
public void createScene() {
createBackground();
createHUD();
createPhysics();
if(!firstTry)
{
createSprites();
firstTry = true;
}
loadLevel(1);
createGameOverText();
loadData();
//levelCompleteWindow = new LevelCompleteWindow(vbom);
setOnSceneTouchListener(this);
}
public void createSprites()
{
updater = new TimerHandler(1f, new ITimerCallback()
{
public void onTimePassed(final TimerHandler pTimerHandler)
{
updateTime();
/*if(timeRemaining <=0)
{
player.onDie();
}*/
pTimerHandler.reset();
}
});
itemMaker = new TimerHandler(5f, new ITimerCallback()
{
public void onTimePassed(final TimerHandler pTimerHandler)
{
createItem();
pTimerHandler.reset();
}
});
enemyMaker = new TimerHandler(2.5f, new ITimerCallback()
{
public void onTimePassed(final TimerHandler pTimerHandler)
{
createEnemy();
pTimerHandler.reset();
}
});
player = new Player(SCREEN_WIDTH/10,SCREEN_HEIGHT-ResourceManager.getInstance().player_region.getHeight(), vbom, camera, physicsWorld)
{
@Override
public void onDie() {
// TODO Auto-generated method stub
if (!gameOverDisplayed)
{
displayGameOverText();
saveData();
createMenuChildScene();
//Log.d("TEST","CALLED");
physicsWorld.unregisterPhysicsConnector(this.fall);
//unregisterUpdateHandler(updater);
//unregisterUpdateHandler(itemMaker);
//unregisterUpdateHandler(enemyMaker);
//unregisterUpdateHandler(increase);
}
}
};
this.registerUpdateHandler(updater);
this.registerUpdateHandler(itemMaker);
this.registerUpdateHandler(enemyMaker);
//this.registerUpdateHandler(increase);
}
当播放器在没有重新加载类
的情况下重新启动场景时,会调用此方法private void cleanUp()
{
this.detachChildren();
menuChildScene.back();
timeRemaining = 30;
totalTime = 0;
isRunning=false;
gameOverDisplayed = false;
//Log.d("TEST","PLayer Y:"+ player.getY());
//player.setY(SCREEN_HEIGHT-ResourceManager.getInstance().player_region.getHeight());
//player.
//updater.reset();
//itemMaker.reset();
//enemyMaker.reset();
//increase.reset();
this.unregisterUpdateHandler(updater);
this.unregisterUpdateHandler(itemMaker);
this.unregisterUpdateHandler(enemyMaker);
//this.unregisterUpdateHandler(increase);
Log.d("TEST","Handlers Unregistered");
//physicsWorld.registerPhysicsConnector(player.fall);
player = new Player(SCREEN_WIDTH/10,SCREEN_HEIGHT-ResourceManager.getInstance().player_region.getHeight(), vbom, camera, physicsWorld)
{
@Override
public void onDie() {
// TODO Auto-generated method stub
if (!gameOverDisplayed)
{
displayGameOverText();
saveData();
createMenuChildScene();
//Log.d("TEST","CALLED");
physicsWorld.unregisterPhysicsConnector(this.fall);
//unregisterUpdateHandler(updater);
//unregisterUpdateHandler(itemMaker);
//unregisterUpdateHandler(enemyMaker);
//unregisterUpdateHandler(increase);
}
}
};
//this.unregisterUpdateHandlers(pUpdateHandlerMatcher);
}
如果需要,我会添加更多代码,但这些是引用TimerHandler的唯一实例。正如你所看到的,我曾尝试多次取消注册计时器,但结果是相同的,计时器直到第二次通过/死亡才被删除
编辑:这是全班(有点乱,有点难读) https://gist.github.com/powerbot99/6030edb3e4c1e84a6200