我正在尝试找出一种方法来从一个级别导航到另一个级别3次。我有3个游戏关卡,我正在尝试升级,从1级到2级,从2级到3级。现在它从1级到3级,然后再从3级到3级。我知道我在哪里做错了,有人能帮我吗:
我的3个级别扩展了我的GameLevel
类,我的主类是Game
,其中创建了goToNextLevel
方法(它在填充当前级别时停止当前级别和向上的级别)。
package game;
public class Game {
/** The World in which the bodies move and interact. */
private GameLevel world;
/** A graphical display of the world (a specialized JPanel). */
private UserView view;
private int level;
private Controller controller;
private SoundClip gameMusic;
/** Initialize a new Game. */
public Game() {
// make the world
level = 1;
world = new Level1();
world.populate(this);
try {
gameMusic = new SoundClip("data/backgroundaudio.wav"); // Open an audio input stream
gameMusic.loop(); // Set it to continous playback (looping)
} catch (UnsupportedAudioFileException|IOException|LineUnavailableException e) {
System.out.println(e);
}
// make a view
view = new MyView(world, world.getPlayer(), 1250, 700);
// uncomment this to draw a 1-metre grid over the view
// view.setGridResolution(1);
// display the view in a frame
JFrame frame = new JFrame("Collect All The Snowballs And Pick Up The Keys!");
// 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
ColourPanel colourPanel = new ColourPanel(view);
frame.add(colourPanel, BorderLayout.WEST);
frame.setResizable(false);
// size the game window to fit the world view
frame.pack();
// make the window visible
frame.setVisible(true);
// get keyboard focus
frame.requestFocus();
// give keyboard focus to the frame whenever the mouse enters the view
view.addMouseListener(new GiveFocus(frame));
controller = new Controller(world.getPlayer());
frame.addKeyListener(controller);
// start!
world.start();
}
/** The player in the current level. */
public SnowCollector getPlayer() {
return world.getPlayer();
}
/** Is the current level of the game finished? */
public boolean isCurrentLevelCompleted() {
return world.isCompleted();
}
/** Advance to the next level of the game. */
public void goNextLevel() {
world.stop();
if (level == 3) {
System.exit(0);
} else {
level++;
// get a new world
world = new Level2();
// fill it with bodies
world.populate(this);
// switch the keyboard control to the new player
controller.setBody(world.getPlayer());
// show the new world in the view
view.setWorld(world);
world.start();
}
}
/** Run the game. */
public static void main(String[] args) {
new Game();
}
}
下面是我的GameLevel
类,我的3个级别中的每一个类都扩展了这个类
/**
* A level of the game.
*/
public abstract class GameLevel extends World {
private SnowCollector player;
public SnowCollector getPlayer() {
return player;
}
/**
* Populate the world of this level.
* Child classes should this method with additional bodies.
*/
public void populate(Game game) {
player = new SnowCollector(this);
player.setPosition(startPosition());
Door door = new Door(this);
door.setPosition(doorPosition());
door.addCollisionListener(new DoorListener(game));
}
/** The initial position of the player. */
public abstract Vec2 startPosition();
/** The position of the exit door. */
public abstract Vec2 doorPosition();
/** Is this level complete? */
public abstract boolean isCompleted();
}
这段代码是当我的主要角色与门接触时调用goToNextLevel
方法的地方
package game;
/**
* Listener for collision with a door. When the player collides with a door,
* if the current level is complete the game is advanced to the next level.
*/
public class DoorListener implements CollisionListener {
private Game game;
public DoorListener(Game game) {
this.game = game;
}
@Override
public void collide(CollisionEvent e) {
SnowCollector player = game.getPlayer();
if (e.getOtherBody() == player && game.isCurrentLevelCompleted()) {
System.out.println("Going to next level...");
**game.goNextLevel();**
}
}
}
我知道问题出在顶部的game.goToNextLevel()
方法上。
https://stackoverflow.com/questions/36036060
复制相似问题