AndEngine TimerHandler - onTimePassed

时间:2015-02-04 17:38:45

标签: timer andengine

我正在使用AndEngine来创建正在发射的射弹的物理模拟。在模拟时,我想绘制比喻的轨迹。 为此,我根据弹丸(sPlayer)的位置每秒画一个正方形。

time_handler=new TimerHandler(1, true,  new ITimerCallback() {


@Override
public void onTimePassed(TimerHandler pTimerHandler) {

    if(simulationOn){ // every 1 second if the simulation is on
        int px=(int)sPlayer.getSceneCenterCoordinates()[0];
        int py=(int)sPlayer.getSceneCenterCoordinates()[1];
        parabola_point=new Rectangle(px, py,4, 4,getVertexBufferObjectManager());
        parabola_point.setColor(Color.WHITE);
        if(!highest_point_found){ //if highest point not found, check it
            float difY =  (float) Math.floor(Math.abs(body.getLinearVelocity().y)) ;

            if(Float.compare(0f, difY) == 0){ // if it is the highest point
                highest_point_found=true;
                drawPointText(); //draw the positions on the scene
                parabola_point=new Rectangle(px, py,16, 16,getVertexBufferObjectManager());
                parabola_point.setColor(Color.RED); // paint this point red


            }
        }


        parabola.add(parabola_point);
        scene.attachChild(parabola_point);
    }



     //  pTimerHandler.reset();

    }
});

我使用的是FixedStepEngine:

  @Override
public Engine onCreateEngine(final EngineOptions pEngineOptions) {
return new FixedStepEngine(pEngineOptions, 50);
}

问题是:

我不知道为什么onTimePassed被调用的速度超过1秒。几秒后就会发生。

我读到的问题,因为FixedStepEngine正在改变' onTimePassed'叫做。如何解决?

1 个答案:

答案 0 :(得分:1)

在我看来,你不会取消注册你的计时器处理程序,导致它们相互交叉。尝试取消注册pTimerHandler