简体   繁体   中英

How do I switch between worlds/levels once certain criteria has been met?

For example I have built one level and there are 35 coins, so how do I say in code the level is over switch to a new world once all the coins have been picked up? I have the other level subclasses I'm not particularly sure how to make the actual game switch levels though?

This is the code for my first level..I have created the other subclasses as GameWorld2 and GameWorld3 with the exact same layout

public class GameWorld extends World {

private Player runningMan;



public GameWorld() {
    super();

    // First Dynamic Body 
       runningMan = new Player(this);
       runningMan.setPosition(new Vec2(-4, -9));

    //LEVEL ONE

    { // make the ground
        Shape shape = new BoxShape(29, 0.5f);
        Body ground = new StaticBody(this, shape);
        ground.setPosition(new Vec2(0, -12.5f));

       // loop to generate coins

         for (int i = 0; i < 35; i++) {
         Body coins = new Coins(this);
         coins.setPosition(new Vec2(i * 1 - 17, 10));
         coins.addCollisionListener(new CoinPickup(runningMan));


(insert repeated code for platforms bla bla have removed it so you guys dont have to see repeated code)


}

public Player getRunningMan() {
    return runningMan;
}
}

Also here is my code for the actual Game class:

/**
 * A world with some bodies.
 */
public class Game {

/** The World in which the bodies move and interact. */
private GameWorld world;

/** A graphical display of the world (a specialised JPanel). */
private MyView view;

/** Initialise a new Demo. */
public Game() {

    // make the world
    world = new GameWorld();

    // make a view
    view = new MyView(world, 1000, 600);



    // add some mouse actions
    // add this to the view, so coordinates are relative to the view
    //view.addMouseListener(new MouseHandler(view));





    // display the view in a frame

    final JFrame frame = new JFrame("Week 1");



    //Link the character to the keyboard

    frame.addKeyListener(new Controller(world.getRunningMan()));




    // quit the application when the game window is closed

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLocationByPlatform(true);       



    // display the world in the window
    frame.add(view);

    // don't let the game window be resized
    frame.setResizable(false);

    // size the game window to fit the world view
    frame.pack();

    // make the window visible
    frame.setVisible(true);



    // start!
    world.start();
}






    /** Run the demo. */
public static void main(String[] args) {
    new Game();
}



}

I would say it could be as simple as having either a coins remaining field in the World a coins collected in the Player class.

However I would recommend looking at your program design. Having a class for each level is unnecessary. An alternative would be to have one class for levels and populate that level with data you can save elsewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM