multiplescreens.adoc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. = multiplescreens
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :relfileprefix: ../../../
  6. :imagesdir: ../../..
  7. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  8. == Implementing Multiple Screens
  9. So, before I explain how to do this, ask yourself a question:
  10. Why the F do I need Multiple Screens?!??
  11. This is JME! That's what AppStates are for.
  12. Now… on with the example of how to implement multiple screens using AppStates.
  13. [source,java]
  14. ----
  15. public class UserLogin extends AbstractAppState {
  16. Main app;
  17. Screen screen;
  18. LoginBox loginWindow;
  19. public UserLogin(Main app, Screen screen) {
  20. this.app = app;
  21. this.screen = screen;
  22. }
  23. @Override
  24. public void initialize(AppStateManager stateManager, Application app) {
  25. super.initialize(stateManager, app);
  26. initLoginWindow();
  27. }
  28. public void initLoginWindow() {
  29. loginWindow = new LoginBox(screen,
  30. "loginWindow",
  31. new Vector2f(screen.getWidth()/2-175,screen.getHeight()/2-125)) {
  32. @Override
  33. public void onButtonLoginPressed(MouseButtonEvent evt, boolean toggled) {
  34. // Some call to the server to log the client in
  35. finalizeUserLogin();
  36. }
  37. };
  38. screen.addElement(loginWindow);
  39. }
  40. @Override
  41. public void cleanup() {
  42. super.cleanup();
  43. screen.removeElement(loginWindow);
  44. }
  45. public void finalizeUserLogin() {
  46. // Some call to your app to unload this AppState and load the next AppState
  47. app.someMethodToSwitchAppStates();
  48. }
  49. }
  50. ----
  51. And Ker-pow! You have a screen without a new Screen.
  52. 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.
  53. [source,java]
  54. ----
  55. effect.setDestroyOnHide(true);
  56. ----