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

2023/2/22

How can I travel between game levels efficiently without a world for "You beat the level"for each level?

GreenFinger GreenFinger

2023/2/22

#
Basically, my game has 3 levels and at the end of each level I want to create a link to another world that shows you beat the level and got 1/2/3 stars. Is there a way I could check in the "You beat the level" world what was the previous world so that I can create a link to the next world? English isn't my first language so here's an example of how I want it to go: Level1 -> YouBeatTheLevel -> Level2 -> YouBeatTheLevel -> Level3
danpost danpost

2023/2/22

#
GreenFinger wrote...
my game has 3 levels and at the end of each level I want to create a link to another world that shows you beat the level and got 1/2/3 stars. Is there a way I could check in the "You beat the level" world what was the previous world so that I can create a link to the next world? here's an example of how I want it to go: Level1 -> YouBeatTheLevel -> Level2 -> YouBeatTheLevel -> Level3
A simple example might be:
import greenfoot.*;

public class LevelCompleted extends World
{
    World completedWorld;
    
    public LevelCompleted(World world)
    {
        completedWorld = world;
        // etc. (build world)
    }
    
    public void act()
    {
        if (/** some condition (key stroked or button clicked) */) proceed();
    }
    
    private void proceed()
    {
        if (completedWorld instanceof Level1) Greenfoot.setWorld(new Level2());
        else if (completedWorld instanceof Level2) Greenfoot.setWorld(new Level3());
    }
}
Just pass the completed world to this world using: From world class : new LevelCompleted(this) From actor class : new LevelCompleted(getWorld())
GreenFinger GreenFinger

2023/2/23

#
Thank you a lot!
You need to login to post a reply.