I'm having trouble collecting 5 coins and successfully going to next world

78 views Asked by At

So we recently started coding in college and I'm having trouble with my game in Greenfoot. I should normally be able to collect 5 coins on level 1, go to the door, and progress to level 2 and so on... This is my piece of code so far:

    private void collect () {
        Actor coin = getOneIntersectingObject(Coin.class);

        if (coin != null) {
            getWorld().removeObject(coin);
            coinsCollected++;
        }
        if (coinsCollected == 5 && fiveCoinsLevel1 == false) {
            if (this.getWorld().getClass() == Level_1.class) {
                getWorld().addObject(new door_temp(), 157, 162);
                fiveCoinsLevel1 = true;
                secondLevel();
                coinsCollected = 0;
            }
        }

        if (coinsCollected == 5 && fiveCoinsLevel2 == false && fiveCoinsLevel1 == true) {
            if (this.getWorld().getClass() == Level_2.class) {
                getWorld().addObject(new door_temp(), 157, 162);
                fiveCoinsLevel2 = true;
                thirdLevel();
            }
        }
    }

After that I have two methods that set the world to either level 2 or 3:

public void secondLevel ()  {
        Actor secondlvl = getOneIntersectingObject(door_temp.class);

        if (secondlvl != null) {
            Greenfoot.setWorld(new Level_2());
        }
    }

    public void thirdLevel ()  {
        Actor thirdlvl = getOneIntersectingObject(door_temp.class);

        if (thirdlvl != null) {
            Greenfoot.setWorld(new Level_3());
        }
    }

Now for some reason, once I collect all 5 coins on level 1 and reach the door to go to level 2, it sends me straight to level 3.

Thanks to anyone who can help me :)

0

There are 0 answers