This site requires JavaScript, please enable it in your browser!
Greenfoot back
1920
1920 wrote ...

2015/4/6

Switch Worlds Using Score?

1920 1920

2015/4/6

#
I am making a game where eating things increases my counter (scoreboard) I have different worlds (levels) In which I wish my main character to go to once it has reached a certain amount of things eaten, say ten. Can anyone show me what code they would use for such thing?
Super_Hippo Super_Hippo

2015/4/6

#
private int thingsEaten = 0;

public void act()
{
    //turning and moving
    if (isTouching(Thing.class))
    {
        removeTouching(Thing.class);
        thingsEaten++;
        if (thingsEaten==10)
        {
            //advance to next level
        }
    }
}
1920 1920

2015/4/7

#
@SupperHippo thanks that worked great! The only problem is that I can only go to the next level and not the rest (it goes from level 1, to level 2, and then once I eat 10 things then it goes back to level 2 ) can you please tell me how to fix this? -sorry I'm new to coding
danpost danpost

2015/4/7

#
For line 12 above use something like this:
if (getWorld() instanceof Level1) Greenfoot.setWorld(new Level2());
else if (getWorld() instanceof Level2) Greenfoot.setWorld(new Level3());
else // etc.
1920 1920

2015/4/7

#
Everything worked perfectly! Thanks guys :)
You need to login to post a reply.