Is it possible to have own setup() and draw() for different scenes?

In my game, there are multiple scenes. I want to define different scenes in different classes. I don't want to make the game load everything in the beginning and only load when the scene is loaded. Can I make their own setup() and draw() for those classes? If yes, how can I call those setup() and draw() in the main program? If no, how can I make the code to make the game work as said above?

Tagged:

Answers

  • edited December 2014 Answer ✓

    this can be achieved easily.

    setup and draw are fixed names though, so you need to work around them.

    What you do: have a int sceneCount - you'll often see it in the forum as state.

    in setup() do only what is done once (size) and everything for scene 0

    now in draw use

    switch (sceneCount) { 
    

    and go to different functions from here : drawForSceneZero, etc.

    once you need to step from scene 0 to 1 you you increase sceneCount by one and call setupForScene1 etc.

    • to make this work you need this approach also in keyPressed, mousePressed etc. : switch (sceneCount) {

    unless all keys do the same in all scenes

    when you have a startScreen or a helpScreen, these are also scenes (or treated as one)

    finally instead of saying scene 0, 1, 2 etc. you can give them names

    final int sceneHobbington = 0;
    final int sceneAtTheMountain = 1;
    // etc. - values that sceneCount can have
    

    ;-)

Sign In or Register to comment.