簡體   English   中英

滿足特定條件后,如何在世界/級別之間切換?

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

例如,我建立了一個關卡,共有35個硬幣,那么當所有硬幣都被拾起后,如何用代碼說到將關卡切換到新世界呢? 我還有其他級別的子類,但我不確定如何使實際的游戲切換級別如何?

這是我第一個級別的代碼。.我以完全相同的布局創建了其他子類GameWorld2和GameWorld3

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;
}
}

這也是我用於實際Game類的代碼:

/**
 * 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();
}



}

我想說,就像在World中剩下一個硬幣字段或在Player類中收集硬幣一樣簡單。

但是我建議您看一下您的程序設計。 每個級別都有一個類是不必要的。 一種替代方法是為級別創建一個類,然后使用可以保存在其他位置的數據填充該級別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM