使用JFrame开发java游戏时的冲突(交叉)问题

yhuiod9q  于 5个月前  发布在  Java
关注(0)|答案(2)|浏览(62)

我正在做一个2d java游戏,玩家需要射击瓷砖。我在代码中遇到了一个错误,子弹会摧毁附近的瓷砖,但并不总是正确的。
PROBLEM VIDEO RECORD
你看,我想要子弹摧毁它所接触的瓷砖,然后在上,左,右,下......你知道了。这是我的代码,我想我总是搞砸了一些事情。
在我看来,错误在于玩家类,因为它处理了销毁。
Player.java的简短版本:(无导入等)。

public class Player{
    public int pointX;
    public int pointY;
    public int velocityX;
    public int defaultVelX = 15;
    public int cooldown = 2;
    public boolean shooting = false;
    public Tile hitTile = null;
    YeahGame yg;
    Shot shot;
    TileManager tm;

    public Player(int x, int y, YeahGame yg) {
        this.pointX = x;
        this.pointY = y;
        this.yg = yg;
        tm = yg.tm;
    }
    public void shoot() {
        if (!shooting) {
            shot = new Shot(pointX, pointY, yg);
            shooting = true;
        }
    }
    public void Hit(int offsetX, int offsetY, int level) {
        Tile tl = tm.GetTile(hitTile.x + offsetX, hitTile.y + offsetY);
        if (tl != null && tl.isActive) {
            if (level >= tl.level) {
                tl.isActive = false;
            } else {
                tl.level -= level;
            }
        }
    }
    public void DestroyTiles() {
        Hit(0,0,1);
        Hit(1,0,1);
        Hit(0,1,1);
        Hit(-1,0,1);
        Hit(0,-1,1);
        if (cooldown > 0) {
            cooldown--;
        } else if (cooldown == 0) {
            tm.NewLayer();
            cooldown = 2;
        }
    }
    public boolean bulletCollision() {
        boolean is = false;
        if (shooting) {
            for (ArrayList<Tile> sampleRow : tm.rows) {
                for (Tile tile : sampleRow) {
                    if (tile.isActive) {
                        Rectangle shotRect = new Rectangle(shot.pointX+yg.tileSize/4, shot.pointY+yg.tileSize/4, yg.tileSize/2, yg.tileSize/2);
                        Rectangle tileRect = new Rectangle(tile.x * yg.tileSize, tile.y * yg.tileSize, yg.tileSize, yg.tileSize);

                        if (tileRect.intersects(shotRect)) {
                            is = true;
                            hitTile = tile;
                            break;
                        }
                    }
                }
                if (is) {
                    break;
                }
            }
        }
        return is;
    }
    public void update() {
        if (shooting) {
            shot.update();
            if (bulletCollision()) {
                DestroyTiles();
                shooting = false;
                shot = null;
            }
            else if (shot.pointY < 0) {
                shooting = false;
                shot = null;
            }
        }
    }
    public void draw(Graphics g) {
        g.setColor(Color.red);
        g.fillRect(pointX, pointY, yg.tileSize, yg.tileSize);
        if (shooting) {
            shot.draw(g);
        }
    }
}

字符串

63lcw9qa

63lcw9qa1#

看起来当你的子弹击中时,它不会记录以前被摧毁的瓷砖。
你的代码的主要问题是,你没有按照职责正确地分离你的代码,这混淆了你和任何试图帮助你的人的信息流。
球员不应该负责清理场地,这是比赛场地本身应该做的。
你应该有一个内部表示你的比赛场地,当一个游戏滴答发生,你可以先做你的物理模拟,然后把它渲染到屏幕上。
并尝试保持您的命名方案,看看Java naming conventions
对于游戏设计,我可以推荐Robert Nystrom的《游戏编程模式》一书。

vjhs03f7

vjhs03f72#

好了,伙计们,问题出在一个函数上,我不小心没有包含在我的代码中。下面是代码的错误部分:

for (ArrayList<Tile> sampleRow : rows) {
 for (Tile tile : sampleRow) {
  if (tile.isActive) {
   tile.y += 1;
   if (tile.y == (yg.height/yg.tileSize)-yg.tileSize) {
    yg.failed = true;
   }
  }
 }
}

字符串
你看,它把所有的瓷砖都向下移动,除了禁用的瓷砖。它们彼此重叠,阻止了碰撞检测,所以我把它做得每个瓷砖都向下移动,即使是禁用的瓷砖。

for (ArrayList<Tile> sampleRow : rows) {
 for (Tile tile : sampleRow) {
  tile.y += 1;
  if (tile.isActive) {
   if (tile.y == (yg.height/yg.tileSize)-yg.tileSize) {
    yg.failed = true;
   }
  }
 }
}

相关问题