繁体   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