2014-06-19 99 views
0

我有精靈(精靈1)和倒數計時器,我想知道是否有辦法讓引擎啓動倒數計時器只有當精靈被感動。如何在觸摸精靈時啓動倒數計時器

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(); 

} 
} 

回答

0

通話計時器就在你的精靈的被覆蓋的onareatouched方法

+0

我怎麼叫計時器,然後,你可以請給我一個例子關於如何去做 – user3661470

0

ü應該在overrided 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)); 
      } 
      } 
     }); 
     } 
     };