Question

I'm trying to get this basic app to detect the overlap of two sprites but nothing is just happening. Following a tutorial on this but it doesn't seem to be working for me. Any ideas?

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();

}
Was it helpful?

Solution

Here:

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

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

            }
        }

You are checking the collision only when the Rectangle is removed when below the screen. Do it like this:

        if (car.overlaps(car2)) {
            System.out.println("Works");
            iter.remove();
        }
        if (car2.y + 200 < 0) {
            iter.remove();
            score++;
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top