简体   繁体   English

满足特定条件后,如何在世界/级别之间切换?

[英]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.

相关问题 一旦达到特定的障碍,如何停止动画? - How do I stop an animation once a certain barrier has been reached? 达到某个值后如何触发声音 - How to trigger a sound once after a certain value has been reached 我如何知道该应用是否已从 Google Play 下载过一次 - How do I know if the app has been downloaded from Google Play once Java我的应用程序再次启动后如何保存背景色 - Java how do I save a background color once my application has been launched again 如果满足特定条件,如何更改 Java 中字符串中的元音? - How do I change vowels in a string in Java if a certain condition is met? 如何让我的 Java 程序在读取到某个频率后立即停止捕获音频? - How do I make my Java program stop capturing audio as soon as a certain frequency has been read? 达到一定分数后如何更改等级? (Java、NetBeans) - How do I change my level after a certain score has been reached? (Java, NetBeans) 当满足某些条件时,如何忽略客户端程序可能添加的任何KeyListener? - How can I ignore any KeyListeners that may have been added by client programs when certain conditions are met? 满足条件后,如何让我的脚本运行另一个脚本? (Java) - How can I have my script run another script when a condition has been met? (Java) 如何在满足某个条件之前多次执行if行 - How to execute an if line more than once until a certain condition is met
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM