-1

我检查了以下链接:

http://www.ehow.com/how_12134402_detect-rectangle-collision-java.html

我已经在我的播放器和房子周围制作了 2 个矩形,但对我的 if 语句应该是什么样子感到困惑,我在 x 上设置了一个布尔值,这意味着如果我的矩形相交 x 将返回 true,所以我知道我从

if(x=true){
    //what to type in here for my collision?
}

我的 2D 状态更改游戏需要这种碰撞,我有一个玩家通过按键输入和地图上的房子四处移动,我希望我的玩家不能穿过房子。

先感谢您。

4

1 回答 1

1

用于Rectangle2D.Double rect = new Rectangle2D.Double(x,y,w,h))定义您的碰撞盒。

然后检查

rect.contains(x,y);

或者

bool isCollision = rectOne.intersects(rectTwo);

或者更完整的例子

// returns true at the first collision
// returns false if no collision with none of the houses
Rectangle2D.Double player = new Rectangle2D.Double(x,y,w,h);
Rectangle2D.Double[] houses = map.getHouseBounds();
boolean isAnyCollision = false;
int i = 0;
while (!isAnyCollision && cnt < houses.length) {
   isAnyCollision = player.intersects(houses[i]);
}
return isAnyCollision;
于 2012-12-13T21:02:31.440 回答