在MIDP1.0中,我们不能像MIDP2.0中的Sprite类一样有很方便的碰撞函数可以使用,我们只能自己来写代码实现。关于矩形碰撞检测,最更简单的方式就是判断一个矩形的4条边是否在另一个矩形的4条边之外。因此我们可以写一个更加通用快速的
简单的碰撞方法:
/**
* 较为通用的矩形碰撞检测方法
* @param ax a矩形左上角x坐标
* @param ay a矩形左上角y坐标
* @param aw a矩形宽度
* @param ah a矩形高度
* @param bx b矩形左上角x坐标
* @param by b矩形左上角y坐标
* @param bw b矩形宽度
* @param bh b矩形高度
* @return
*/
public static final boolean isIntersectingRect(int ax, int ay, int aw,
int ah, int bx, int by, int bw, int bh) {
if (by + bh < ay || // is the bottom of b above the top of a?
by > ay + ah || // is the top of b below bottom of a?
bx + bw < ax || // is the right of b to the left of a?
bx > ax + aw) // is the left of b to the right of a?
return false;
return true;
}
这样速度会快很多。对于有透明背景的图片,我们可以围绕非透明部分多设立几个矩形区进行碰撞检测。