I'm not 100% sure why I need to use interfaces, I assume it's for simplicity.
You need some common type to assign a screen to a variable, so you need to extend from a base class or implement a common interface.
Use case is something like that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| MainMenu mainMenu = new MainMenu(game); Highscore highscore = new Highscore(game); Level level1= new Level1(game); Level level2= new Level2(game); ... Level levelN= new LevelN(game);
Screen currentlyShowing;
...
switch(nextScreen) { case MAINMENU: currentlyShowing=mainMenu; inputManager.delegateInputTo(mainMenu); break; case LEVEL1: currentlyShowing=level1; inputManager.delegateInputTo(level1); break; case LEVEL2: currentlyShowing=level2; inputManager.delegateInputTo(level2); break; case LEVELN: currentlyShowing=levelN; inputManager.delegateInputTo(levelN); break; case HIGHSCORE: currentlyShowing=highscore; inputManager.delegateInputTo(highscore); break; } }
...
while(notExited) { currentlyShowing.update(); currentlyShowing.render(renderTarget); sync(); } |
And how would I share variables between these?
Thats the purpose of passing game to each screen, so you can read out and manipulate common variables contained in the game class.