[英]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? 例如,我建立了一个关卡,共有35个硬币,那么当所有硬币都被拾起后,如何用代码说到将关卡切换到新世界呢? 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 这是我第一个级别的代码。.我以完全相同的布局创建了其他子类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;
}
}
Also here is my code for the actual Game class: 这也是我用于实际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();
}
}
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. 我想说,就像在World中剩下一个硬币字段或在Player类中收集硬币一样简单。
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. 一种替代方法是为级别创建一个类,然后使用可以保存在其他位置的数据填充该级别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.