用.overlaps方法检测碰撞/重叠

时间:2014-02-22 09:46:32

标签: java android libgdx collision overlap

我正在尝试使用这个基本的应用来检测两个精灵的重叠,但没有任何事情发生。按照这方面的教程,它似乎并没有为我工作。有什么想法吗?

Relevent-ish code

   Rectangle car;
    Array<Rectangle> carb;

public void render() {

        String scorer = String.valueOf(score);
        camera.update();
        batch.setProjectionMatrix(camera.combined);

        Iterator<Rectangle> iter = carb.iterator();
        while (iter.hasNext()) {
            Rectangle car2 = iter.next();
            car2.y -= score + 200 * Gdx.graphics.getDeltaTime();
            if (car2.y + 200 < 0) {
                iter.remove();
                score++;

                if (car.overlaps(car2)) {
                    System.out.println("Works");
                    iter.remove();

                }
            }
        }


private void spawnCar() {

    Rectangle car2 = new Rectangle();


    car = new Rectangle();

    lastSpawn = TimeUtils.nanoTime();

}

1 个答案:

答案 0 :(得分:2)

下面:

        if (car2.y + 200 < 0) {
            iter.remove();
            score++;

            if (car.overlaps(car2)) {
                System.out.println("Works");
                iter.remove();

            }
        }

仅在屏幕下方移除矩形时才检查碰撞。这样做:

        if (car.overlaps(car2)) {
            System.out.println("Works");
            iter.remove();
        }
        if (car2.y + 200 < 0) {
            iter.remove();
            score++;
        }