我目前正在研究一个预建的Java游戏引擎,但我已经重写了碰撞系统,以使用玩家相对于瓷砖的位置以及碰撞的矩形。我需要能够检查玩家是否与任何东西发生碰撞以允许玩家再次移动,但在我的方法中,我只能检查玩家是否发生碰撞。
protected void tileCollision()
{
int calledNumTimes = 0;
AnimatedSprite player = findPlayer();
Tile lastCollided;
for(int i = 0; i < _sprites.size(); i++)
{
calledNumTimes++;
AnimatedSprite spr = _sprites.get(i);
for(int j = 0; j < tileWithinRange.length; j++)
{
Tile tile = tileWithinRange[j];
if(tile != null && spr.collidesWith(tile.getBounds()))
{
lastCollided = tile;
player.setCollided(true);
calledNumTimes++;
if(!spr.hasMultBounds() && !tile.hasMultBounds())
{ //tile and spr have only one bound
//spr.setBounds(16, spr.getBounds().x + 5, spr.getBounds().y);
if(tile.getTileBounds().intersects(player.getBounds()))
{
tileCollision(spr, tile, -1, -1);
System.out.println("colliding");
} else {
System.out.println("nope");
}
// spr.setBounds(16, spr.getBounds().x - 5, spr.getBounds().y);
}
} else if(tile != null && !spr.collidesWith(tile.getBounds()))
player.setCollided(false);
}
} //end _tiles for loop
} //end _sprites for loop
截至目前,
System.out.println("nope");
从不打印,所以我只能检查玩家是否在碰撞而不能检查他是否没有。
答案 0 :(得分:0)
直到你放弃,问题就会明显:
protected void tileCollision()
{
int calledNumTimes = 0;
AnimatedSprite player = findPlayer();
Tile lastCollided;
for(int i = 0; i < _sprites.size(); i++)
{
calledNumTimes++;
AnimatedSprite spr = _sprites.get(i);
for(int j = 0; j < tileWithinRange.length; j++)
{
Tile tile = tileWithinRange[j];
if(tile != null && spr.collidesWith(tile.getBounds()))
{
lastCollided = tile;
player.setCollided(true);
calledNumTimes++;
if(!spr.hasMultBounds() && !tile.hasMultBounds())
{ //tile and spr have only one bound
//spr.setBounds(16, spr.getBounds().x + 5, spr.getBounds().y);
computeTilePlayerCollision(spr, tile, player);
// spr.setBounds(16, spr.getBounds().x - 5, spr.getBounds().y);
}
} else if(tile != null && !spr.collidesWith(tile.getBounds()))
player.setCollided(false);
}
} //end _tiles for loop
} //end _sprites for loop
private void computeTilePlayerCollision(AnimatedSpr spr, Tile tile, AnimatedSprite player) {
if(tileCollidesWithPlayer(tile, player)) {
reportCollision();
} else {
reportNonCollision();
}
}
private boolean tileCollidesWithPlayer(Tile tile, AnimateSprite player) {
tile.getTileBounds().intersects(player.getBounds())
}
private void reportCollision(AnimatedSprite spr, Tile tile) {
tileCollision(spr, tile, -1, -1);
System.out.println("colliding");
}
private void reportNonCollision(AnimatedSprite spr, Tile tile) {
System.out.println("nope");
}
继续这个ad-absurdum,直到虫子显而易见,因为意大利面无处藏身;)