0

I have a sprite(sprite1), and a countdown timer, and I wanted to know if there is a way to have andengine start the countdown timer ONLY when the sprite is touched.

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 Answers2

0

call the timer just in the overriden onareatouched method of your sprite

Izu
  • 235
  • 1
  • 5
0

U should have the timer in the overrided onAreaTouched. Everything in the method onAreaTouched will only be called when you touch the sprite, try something like this (haven't checked the code properly):

        @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));
            }
           } 
        });
        }
       };