一旦满足某些标准,我如何在世界/水平之间切换?

时间:2014-03-04 14:13:34

标签: java

例如我已经构建了一个级别,并且有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;
}
}

这里是我的实际游戏类的代码:

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



}

1 个答案:

答案 0 :(得分:0)

我想说这可能就像在世界上有一个硬币剩余字段在玩家类中收集硬币一样简单。

但是我建议您查看您的程序设计。每个级别都有一个班级是不必要的。另一种方法是为级别设置一个类,并使用可以在其他地方保存的数据填充该级别。