我正试图在Bitmap
的屏幕onTouch
周围移动Rect
。当然,我不希望图像消失,或者离开屏幕,所以我为屏幕的每一面,顶部和底部创建一个Rect。 我为我美丽的艺术作品道歉。
onTouch
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Left Key Touched
if (x <= bLeftRect.right && x >= bLeftRect.left
&& y >= bLeftRect.top && y <= bLeftRect.bottom) {
hero.setX(hero.getX() - tileDimen);
heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
if (heroRect.intersect(wallTopRect)) {
hero.setX(hero.getX() + tileDimen);
} else if (heroRect.intersect(wallLeftRect)) {
hero.setX(hero.getX() + tileDimen);
}
//etc....
是否有更好的方法,因为我将有更多需要碰撞的对象。我在考虑创建1“外围”Rect
:
//Right key touched
if (perimeterRect.contains(heroRect)) {
hero.setX(hero.getX() + tileDimen);
} else {
//do nothing (collision)
}
这会是一种更好的方法吗?非常感谢任何建议/见解。
谢谢,
马特
答案 0 :(得分:0)
想出来:
检查heroRect是否在每个MotionEvent
的perimeterRect内部更简单,如果heroRect位于周边内部,则检查heroRect是否与任何其他Rect
碰撞
我的onTouch方法:
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Left Key Touched
if (x <= bLeftRect.right && x >= bLeftRect.left
&& y >= bLeftRect.top && y <= bLeftRect.bottom) {
hero.setX(hero.getX() - tileDimen);
heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
if (!(perimeterRect.contains(heroRect))) {
hero.setX(hero.getX() + tileDimen);
heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
} //else if hero collides with other Rects... {
// }
// Up Key Touched
} else if (x <= bUpRect.right && x >= bUpRect.left
&& y >= bUpRect.top && y <= bUpRect.bottom) {
hero.setY(hero.getY() - tileDimen);
heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
if (!(perimeterRect.contains(heroRect))) {
hero.setY(hero.getY() + tileDimen);
heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
} //else if hero collides with other Rects... {
// }
// Right Key Touched
} else if (x <= bRightRect.right && x >= bRightRect.left
&& y >= bRightRect.top && y <= bRightRect.bottom) {
hero.setX(hero.getX() + tileDimen);
heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
if (!(perimeterRect.contains(heroRect))) {
hero.setX(hero.getX() - tileDimen);
heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
} //else if hero collides with other Rects... {
// }
// Down Key Touched
} else if (x <= bDownRect.right && x >= bDownRect.left
&& y >= bDownRect.top && y <= bDownRect.bottom) {
hero.setY(hero.getY() + tileDimen);
heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
if (!(perimeterRect.contains(heroRect))) {
hero.setY(hero.getY() - tileDimen);
heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
} //else if hero collides with other Rects... {
// }
} else {
//button was not pressed, do nothing
}
break;
}
return true;
}
现在位图不会超过周长Rect:)