Java Game Leveling Up

时间:2016-03-16 12:44:27

标签: java

我正试图找出一种从一个级别导航到另一个级别的方法3次。我有3个游戏关卡,我正在尝试从1级升级到2级,从2级升级到3级。现在它从1级升级到3级,然后再从3级升级到3级。我知道我在某个地方出错,任何人都可以帮忙:

我的3个级别扩展了我的GameLevel类,我的主要类是Game,其中创建了goToNextLevel方法(在填充时停止当前级别并升级)。 / p>

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()方法。

2 个答案:

答案 0 :(得分:0)

world = new Level2();

创建Level2类型的世界。我不认为这是你想要的,因为这意味着每个等级再次达到同一水平(但只有两次)

虽然它仍然不会按照你描述的方式做出反应......除非你从Level0或其他东西开始,否则你的描述就会如此。

答案 1 :(得分:0)

public class Main {
public static int l1 = 50;
public static int l2 = 105;
public static int l3 = 170;
public static int l4 = 210;
public static int pl = 0;
public static void main(String[] args) {
    level_System();
}
public static void level_System() {
    pl = 55;
    if (pl >= l1) {
        pl -= l1;
        System.out.print("you leveled up\nLEVEL: 2\nxp left ");
        System.out.println(l2 - pl);
        System.out.print("current xp " + pl);
    } else if (pl >= l2) {
        System.out.println("you leveled up\nLEVEL: 3\nxp left ");
    } else if (pl >= l3) {
        System.out.println("you leveled up\nLEVEL: 4\nxp left ");
    } else if (pl >= l4) {
        System.out.println("you leveled up\nLEVEL: 5\nxp  left ");
    }

}
}

您不必将public包含在您的静态int中,我只是这样做,因为您永远不会知道