如何在触摸精灵时启动倒数计时器

时间:2014-06-19 03:50:39

标签: android andengine sprite countdowntimer

我有一个精灵(sprite1)和一个倒数计时器,我想知道是否有办法在触摸精灵时启动倒数计时器 ONLY

Sprite sprite1; 
int time=10; 
@Override
public void onPopulateScene(Scene pScene,
        OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {

    sprite1= new Sprite(100, 100, squarer, this.mEngine.getVertexBufferObjectManager())

            {

            @Override
            public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y)  
            { 
                scene.setBackground(new Background(200,200,200));

            }

            };  
    TimerHandler mtimer= new TimerHandler(0.1f,true,new ITimerCallback() {

        @Override
        public void onTimePassed(TimerHandler pTimerHandler) {
            // TODO Auto-generated method stub 
            time--; 
            if (time==0){  
                scene.setBackground(new Background(0,0,120));
            }
        } 
  });

            this.scene.attachChild(sprite1); 
            this.scene.registerTouchArea(sprite1); 
            this.scene.registerUpdateHandler(mtimer);
    pOnPopulateSceneCallback.onPopulateSceneFinished();

}
}

2 个答案:

答案 0 :(得分:0)

只需在精灵的覆盖onareatouched方法中调用计时器

答案 1 :(得分:0)

U应该在覆盖的onAreaTouched上有计时器。方法onAreaTouched中的所有内容只有在你触摸精灵时才会被调用,尝试这样的东西(没有正确检查代码):

        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y)  
        { 
            scene.setBackground(new Background(200,200,200));
            TimerHandler mtimer= new TimerHandler(0.1f,true,new ITimerCallback() {

                @Override
                public void onTimePassed(TimerHandler pTimerHandler) {
                // TODO Auto-generated method stub 
                time--; 
            if (time==0){  
                scene.setBackground(new Background(0,0,120));
            }
           } 
        });
        }
       };