Java - 在与播放器发生冲突时从数组中删除对象

时间:2013-01-26 21:57:22

标签: java

我正在研究java中的下降块游戏,你必须在屏幕上移动玩家才能躲避块。每当你被一个块击中时,根据块类型,它将减少或增加一个玩家类中的int。我有一个问题,当玩家被块击中时,int继续向下,直到现在看不见的块在屏幕外。基本上我只需要一种检查对象数组的方法,当一个对象满足指定的条件时(例如(delete == true)),它会将数组中该块的当前位置设置为null。

更新块位置的方法:

public void dropFoods(int speed) {
    for (int x = 699 - speed; x >= 0; x--) {
        for (int y = 0; y < 7; y++) {
            if(x > (699 - GUI.HEIGHT) - 10) {
                food[y][x] = null;
                continue;
            }
            food[y][x + speed] = food[y][x];
            food[y][x] = null;

        }
    }
}

绘制块的方法(食物是不同类型的块):

for(int x = 0; x < food.length; x++) {
            for(int y = 0; y < food[x].length; y ++) {
                Object o = food[x][y];
                if(o instanceof Apple) {
                    new Apple(x * 100, y - Apple.HEIGHT, g);            
                }
                if(o instanceof Burger) {
                    new Burger(x * 100, y - Burger.HEIGHT, g);          
                }
            }
}

检测与玩家碰撞的方法:

if(Food.getHitBox().intersects(Player.hitBox())) {
        willDraw = false;
        Player.weight -= 1;
}

1 个答案:

答案 0 :(得分:0)

您应首先提供更多代码和调试结果。

if(Food.getHitBox().intersects(Player.hitBox())) {
        willDraw = false;
        Player.weight -= 1;
}

所以当某些东西被玩家击中时,你会从地图上移除Food对象吗?因为根据这段代码,你不是。