2014-05-21 44 views
0

我爲Android 4+編寫了一款Android遊戲。遊戲在不同的genymotion模擬器和我的設備上正常工作,這是三星Galaxy GT-6312 os 4.1.2。當我將它安裝在Galaxy S3 Mini GT-I8190T上時4.1.2 getHitsRect函數返回奇怪的結果。每次我從同一視圖調用函數時,我會得到不同大小的矩形,這使得我的遊戲無法玩。我已經將所有視圖填充爲(0,0,0,0),並且使用wrap_content參數表示寬度和高度,但無濟於事。我試圖檢查在ddms中的視圖層次轉儲,但我得到了一個錯誤,雖然我懷疑它可以告訴我一些我不知道的東西。我絕對不知道該怎麼做,任何幫助將不勝感激。在某些Android設備上從getHitsRect返回的隱晦結果

public class CheckShotsHitThread implements Runnable { 
    private volatile ArrayList<ShotController> m_Shots; 
    private volatile RainbowDashController m_RD; 

    public CheckShotsHitThread(GameController[] controllers, RainbowDashController rainbowDashController) { 
     m_Shots = new ArrayList<ShotController>(); 
     for (GameController controller : controllers) { 
      if (controller instanceof ShotController) m_Shots.add((ShotController) controller); 
     } 
     m_RD = rainbowDashController; 
    } 

    @Override 
    public void run() { 
     try { 
      Thread.sleep(3000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     while (GameModel.isRunning) { 
      m_RD.getView().getHitRect(m_RD.mHitRect); 
      for (ShotController shot : m_Shots) { 
       if(shot.isOutOfGame()||shot.getModel().isDead()) continue; 
       shot.getView().getHitRect(shot.mHitRect); 
       if (shot.mHitRect.intersect(m_RD.mHitRect)) kill(shot, m_RD); 
      } 
      try { 
       Thread.sleep(GameModel.ITERATION_PAUSE_TIME); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

     } 
     Log.d("test", "shots thread dead"); 

    } 

    private void kill(ShotController shot, RainbowDashController rd) { 
     Log.d("test","shot width: "+shot.getView().getWidth()+" RD width:"+rd.getView().getWidth()+" hit rect: left: "+m_RD.mHitRect.left+", right: "+m_RD.mHitRect.right); 
     shot.getModel().setDead(true); 
     rd.getModel().setCaptured(true); 
    } 
} 

回答

0

通過重寫getHitRect(矩形outRect)方法

@Override 
public void getHitRect(Rect outRect) { 
    outRect.top=(int)getY()-getHeight()/2+getPaddingBottom()/2;; 
    outRect.bottom=(int)getY()+getHeight()/2-getPaddingBottom()/2;; 
    outRect.left=(int)getX(); 
    outRect.right=(int)getX()+getWidth(); 

} 
解決
相關問題