2013-12-12 63 views

回答

3

您需要覆蓋Sprite的onAreaTouched方法,如下所示。您可以從pSceneTouchEvent,pTouchAreasLocalX和pTouchAreaLocalY變量中獲取有關touch事件的信息,並使用它們來確定移動球的方式。

您不想將任何力施加到OnAreaTouched方法內部的物理主體,儘管因爲應該使用更新處理程序更改物理主體。我建議讓onAreaTouched方法設置一個標誌和一些其他變量,以便下次更新處理程序運行時可以使用這些值。

更新:我添加了一些代碼,以幫助您找出方向。 if語句中的註釋應該解釋它們何時被調用。基本上,你獲得了最初的觸摸位置(動作向下),計算你移動到的位置(動作移動)並使用方向在更新處理器中應用力(動作向上)。

mSprite = new Sprite(x, y ,mRegion, mEngine.getVertexBufferObjectManager()){ 
     @Override 
     public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) 
     { 

      //set flag member variable that sprite has been touched 
      if (pSceneTouchEvent.isActionDown()) 
      { 
       //here is where the touch was initiated so you 
       //can store the x,y location. You obtain it by using pSceneTouchEvent.getX() 
       // and pSceneTouchEvent.getY() 
      } 

      if (pSceneTouchEvent.isActionMove()) 
      { 
       //This will be called when you slide your finger, so you 
       //can get the new coordinates by again using pSceneTouchEvent.getX() 
       // and pSceneTouchEvent.getY() 

      } 

      if (sSceneTouchEvent.isActionUp()) 
      { 
       //this will be called when you release the sprite 
       // and tell the update handler to apply the force 
      } 
     } 
    }; 


this.registerUpdateHandler(new IUpdateHandler(){ 
     @Override 
     public void onUpdate(float pSecondsElapsed) { 
      //if flag is set apply a force to the physics body 
      //set flag to false to wait for next touch event 
     } 

     @Override 
     public void reset() { 

     } 
+0

ThankQ太多了,但我無法獲得滑動方向。 你能幫我一把。 – Uday

0

jteezy的回答是不夠的,因爲你需要處理雪碧中的touchevents。爲了處理場景中的所有觸摸事件,首先你的類必須實現IOnSceneTouchListener。然後你應該重寫public boolean onSceneTouchEvent(Scene pScene,final TouchEvent pSceneTouchEvent)。然後在您的場景中調用並調用該方法yourscene.setOnSceneTouchListener(this);

下面是代碼:

public class GameManager implements IOnSceneTouchListener{ 

    private boolean mSelected = false; 
    private boolean mThrown = false; 
    private float mX; 
    private float mY; 
    private float velX; 
    private float velY; 
    private TouchEvent mTouchEvent[] = {null, null, null, null, null}; 

    callThatMethodSomeWhere(){ 

mScene.setOnSceneTouchListener(this); 

mİşaretçiSprite = new Sprite(0, 0, mİşaretçiTextureRegion, mEngine.getVertexBufferObjectManager()); 

} 

@Override 
    public boolean onSceneTouchEvent(Scene pScene, final TouchEvent pSceneTouchEvent) 
    { 
     if(pSceneTouchEvent.isActionDown() && mİşaretçiSprite.contains(pSceneTouchEvent.getX() , pSceneTouchEvent.getY())){ //This part is so important that you check if the touch w 

      mX = pSceneTouchEvent.getX(); 
      mY = pSceneTouchEvent.getY(); 

      mSelected = true; 
     } 

     else if(pSceneTouchEvent.isActionMove() && mSelected){ 

      if(mTouchEvent[0] == null){ 
       mTouchEvent[0] = pSceneTouchEvent; 
      }else if(mTouchEvent[1] == null){ 
       mTouchEvent[1] = pSceneTouchEvent; 
      }else if(mTouchEvent[2] == null){ 
       mTouchEvent[2] = pSceneTouchEvent; 
      }else if(mTouchEvent[3] == null){ 
       mTouchEvent[3] = pSceneTouchEvent; 
      }else if(mTouchEvent[4] == null){ 
       mTouchEvent[4] = pSceneTouchEvent; 
      }else{ 
       velX = mTouchEvent[4].getX() - mX; 
       velY = mTouchEvent[4].getY() - mY; 

       mThrown = true; 
       mSelected = false; 


       Body.applyForceToCenter(velX*10, velY*10); 


      } 
     } 

     else if (pSceneTouchEvent.isActionUp() && mSelected){ 

      velX = pSceneTouchEvent.getX() - mX; 
      velY = pSceneTouchEvent.getY() - mY; 

      mBody.applyForceToCenter(velX*10, velY*10); 
      mThrown = true; 


     } 

     return false; 
    } 

編輯:我忘了說,你可以增加你的存儲touchevents的數量,但最好是那樣做給玩家在有限的刷卡時間。 也可以將velX和velY乘以更高的值以使其更快。

相關問題