我为android 4+写了一个安卓游戏。游戏在不同的genymotion模拟器和我的设备上工作正常,这是三星Galaxy Young GT-6312 os 4.1.2。当我在Galaxy S3 Mini GT-I8190T操作系统4.1.2上安装它时,函数getHitsRect会返回奇怪的结果。每当我从同一视图调用该函数时,我得到不同大小的矩形,这使我的游戏无法播放。我已将所有视图填充设置为(0,0,0,0)并使用wrap_content参数来表示宽度和高度,但无效。我试图检查ddms中的视图层次结构转储,但我收到了一个错误,虽然我怀疑它可以告诉我一些我还不知道的东西。我完全不知道该怎么做,任何帮助都会受到高度赞赏。
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 :(得分:0)
通过重写getHitRect(Rect 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();
}