首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java游戏升级

Java游戏升级
EN

Stack Overflow用户
提问于 2016-03-16 20:44:27
回答 2查看 1.5K关注 0票数 0

我正在尝试找出一种方法来从一个级别导航到另一个级别3次。我有3个游戏关卡,我正在尝试升级,从1级到2级,从2级到3级。现在它从1级到3级,然后再从3级到3级。我知道我在哪里做错了,有人能帮我吗:

我的3个级别扩展了我的GameLevel类,我的主类是Game,其中创建了goToNextLevel方法(它在填充当前级别时停止当前级别和向上的级别)。

代码语言:javascript
复制
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个级别中的每一个类都扩展了这个类

代码语言:javascript
复制
/**
 * 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方法的地方

代码语言:javascript
复制
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()方法上。

EN

回答 2

Stack Overflow用户

发布于 2016-03-16 21:05:17

代码语言:javascript
复制
world = new Level2();

创建Level2类型的世界。我不认为这是你想要的,因为这意味着每一级都会再次进入同一级(但只有两次)。

尽管它仍然不会像你所描述的那样反应...除非您从Level0或其他语言开始,否则它将与您所描述的完全相同。

票数 0
EN

Stack Overflow用户

发布于 2019-06-26 08:29:33

代码语言:javascript
复制
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 ");
    }

}
}

你不需要在静态int中包含public,我这么做是因为你永远不知道

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36036060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档