【发布时间】:2014-02-25 03:56:23
【问题描述】:
public void update() {
speedY = bg.getSpeedY() / 2;
tileY += speedY;
r.set(tileX, tileY, 50, 48);
// Collision with cycle here:
if (tileY > 480) {
r = null;
}
if (tileY < 480) {
checkCollision();
}
}
private void checkCollision() {
if (type != 0) {
boolean val = Rect.intersects(Cycle.rect, r);
if (val) {
test = true;
}
}
}
使用调试器,我确定所有矩形都正确更新,并且 Cycle.rect 返回正确的矩形位置。如果矩形在第一次检查时相交,它工作正常。但是,随着更新的运行,相交永远不会返回 true。为什么会发生这种情况?我只使用布尔值来帮助调试。
我记录了两个我认为矩形应该相交但不相交的值:
r: 02-24 23:32:50.762: V/矩形 (16624): 矩形 (50, 314 - 50, 48) Cycle.rect: 02-24 23:32:50.762: V/Rect(16624): Rect(49, 329 - 100, 100)
【问题讨论】:
-
仅凭这些信息很难回答。也许
type是0?也许Cycle.rect真的不与r相交?检查Cycle.rect和r的每个位置。此外,记录值tileY可能会有所帮助。 -
我没有记录瓷砖Y,而是记录了两个矩形以确保它们相交。我在类型检查中登录,所以我知道类型不是 0。 02-24 23:32:50.762: V/Rect(16624): Rect(50, 314 - 50, 48) 02-24 23:32:50.762 : V/Rect(16624): Rect(49, 329 - 100, 100) 这两个矩形应该相交吗?
-
检查我的答案。经过一番试验,如果我交换你的上下值,我得到你的
Rects 相交。
标签: java android game-engine intersection rectangles