1.第一种方案,新建PhysicsManager 类,模拟物理引擎的碰撞
_/*
物理引擎
*/
_public class PhysicsManager {
public static boolean isHit(FlySprite one, FlySprite other){
//左侧
if(one.getX() > other.getX() && one.getX() > other.getX() + other.getWidth()){
return false;
}
//右侧
if(one.getX() < other.getX() && other.getX() > one.getX() + one.getWidth()){
return false;
}
//上方
if(one.getY() > other.getY() && one.getY() > other.getY() + other.getHeight()){
return false;
}
//下方
if(one.getY() < other.getY() && other.getY()>one.getY() + one.getHeight()){
return false;
}
//相交(碰撞)
return true;
}
}
2.第二种方案,在FlySprite类中添加碰撞检测方法
public boolean isHit(FlySprite other){
//左侧
if(this.x > other.x && this.x > other.x + other.width){
return false;
}
//右侧
if(this.x < other.x && other.x > this.x + this.width){
return false;
}
//上方
if(this.y > other.y && this.y > other.y + other.height){
return false;
}
//下方
if(this.y < other.y && other.y>this.y + this.height){
return false;
}
//相交(碰撞)
return true;
}