multiplescreens

Implementing Multiple Screens

So, before I explain how to do this, ask yourself a question:

Why the F do I need Multiple Screens?!??

This is JME! That’s what AppStates are for.

Now… on with the example of how to implement multiple screens using AppStates.

public class UserLogin extends AbstractAppState {
    Main app;
    Screen screen;

    LoginBox loginWindow;

    public UserLogin(Main app, Screen screen) {
        this.app = app;
        this.screen = screen;
    }

    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);

        initLoginWindow();
    }

    public void initLoginWindow() {
        loginWindow = new LoginBox(screen,
                "loginWindow",
                new Vector2f(screen.getWidth()/2-175,screen.getHeight()/2-125)) {
            @Override
            public void onButtonLoginPressed(MouseButtonEvent evt, boolean toggled) {
                // Some call to the server to log the client in
                finalizeUserLogin();
            }
        };
        screen.addElement(loginWindow);
    }

    @Override
    public void cleanup() {
        super.cleanup();

        screen.removeElement(loginWindow);
    }

    public void finalizeUserLogin() {
        // Some call to your app to unload this AppState and load the next AppState
        app.someMethodToSwitchAppStates();
    }
}

And Ker-pow! You have a screen without a new Screen.

If you want to be fancy about loading and unloading the contents of the screen, use the EffectManager and call the following when running the effects from the cleanup() method of the AppState.

effect.setDestroyOnHide(true);