# Loading different scenes after time delay

**URL:** https://discourse.processing.org/t/loading-different-scenes-after-time-delay/22275
**Category:** Coding Questions
**Created:** [June 30, 2020, 6:55am UTC](https://discourse.processing.org/t/loading-different-scenes-after-time-delay/22275 "2020-06-30T06:55:20Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Dominantxx](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@Dominantxx](https://discourse.processing.org/u/Dominantxx)
#### Post date: [June 30, 2020, 6:55am UTC](https://discourse.processing.org/t/loading-different-scenes-after-time-delay/22275/1 "2020-06-30T06:55:20Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

So when the code begins to run (play), I want an image to fill the screen. After a time delay, let’s say 5 seconds, I want to load a different scene as in

int sceneNum = 1

If sceneNum == 1 void scene1(){}

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [June 30, 2020, 10:37am UTC](https://discourse.processing.org/t/loading-different-scenes-after-time-delay/22275/2 "2020-06-30T10:37:36Z")

</div>

Hi,

Welcome to the community! 😉

For tracking the time, you can check those functions in the Processing documentation :

- [`millis()`](https://processing.org/reference/millis_.html)
- [`second()`](https://processing.org/reference/second_.html)

This is a more complex example (but very modular if you want to add scenes) :

```auto
ScenePlayer scenePlayer;

void setup(){
  size(500, 500);
  
  // Initialize the scene player
  // With 5000 milliseconds (5 seconds between each scenes)
  scenePlayer = new ScenePlayer(5000);
  
  // Add some scenes
  scenePlayer.addScene(new BlackScene());
  scenePlayer.addScene(new RedScene());
}

void draw(){
  scenePlayer.update();
  scenePlayer.draw();
}

// The ScenePlayer class is holding all the scenes
class ScenePlayer{
  int currentScene;
  ArrayList<Scene> scenes;
  boolean isPlaying = false;
  int slideTime;
  int currentTime;
  
  ScenePlayer(int slideTime){
    scenes = new ArrayList<Scene>();
    this.slideTime = slideTime;
    
    // Initialize the time
    currentTime = millis();
  }
  
  // Add a new scene
  void addScene(Scene scene){
    scenes.add(scene);
  }
  
  void update(){
    // If the delay has ended
    if(millis() - currentTime > slideTime){
      // Move to the next scene
      nextScene();
      // Reset the time
      currentTime = millis();
    }
  }
  
  // Dispay the curren scene
  void draw(){
    scenes.get(currentScene).draw();
    
    // Display the scene counter and the time
    fill(255);
    textSize(20);
    text("Scene n°" + currentScene, 50, 50);
    
    int timeLeft = floor((slideTime - (millis() - currentTime)) / 1000) + 1;
    text("Time left : " + timeLeft + " seconds", 250, 50);
  }
  
  // Move to the next scene
  void nextScene(){
    currentScene = (currentScene + 1) % scenes.size();
  }
}

// The interface is representing a scene
// Each scene has it's own draw() method
interface Scene{
  void draw();
}

// SCENES -------------------------------------

class BlackScene implements Scene{
  void draw(){
    // Black background
    background(0);
  }
}

class RedScene implements Scene{
  void draw(){
    // Red background
    background(255, 0, 0);
  }
}

```

It’s using the following notions :

- [class](https://processing.org/reference/class.html)
- [ArrayList](https://processing.org/reference/ArrayList.html)
- [interface](https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

If you have any questions, let me know!
