Scene Management for Larger Sketches

So one of the problems when making a large sketch (or any type of larger media-rich project) is managing the scenes that the user sees. If the project was a game, then each scene could be a level, or a type of level. For example, the main gameplay could be one scene while bonus stages, the title screen, and cutscenes could be implemented on other scenes. On top of that, what if we want to have a transition between scenes like fading to black or having the Batman logo spin around? This article will discuss a straightforward way to do this using Processing. It’s not a complicated bit of code, but it’s one of many possible solutions.

The Scene Object

First of all, we need a way to reference each of the different scenes/levels/screens/what-have-you in a uniform way. To do this, we use an interface named “Scene” which all level instances will implement. It will mimic the usual Processing sketch in the sense that it needs a “draw” function and any of the standard input functions you’d need for your project. Here is a trimmed down example:

public interface Scene{
public void draw(PGraphics g);
public void startLevel();

Now, the draw function will just have the graphics “magic” passed into it and you do all of your typical draw calls with “g.” prefixed to it. The startLevel function is what we’re going to call to let a level know when it’s about to get used. I’ll leave it up to you to implement the interface to do something interesting (or I’ll do it later if I get enough complaints).

The Scene Manager

Here’s the meat of the project. This will hold a collection of our scenes and let us swap between them whenever we want. The scenes will need some way to access this object. A quick and simple way is to just have it accessible via Singleton pattern.

public class SceneManager{
private HashMap<String,Scene> sceneCollection = null;
public Scene(){
sceneCollection = new HashMap<String,Scene>();
public void addScene(String sceneName, Scene scene){
sceneCollection.put( sceneName, scene );
public void changeScene(Scene scene){
currentScene = scene;


Using the SceneManager

At this point you just need to add your list of scenes to the manager and then call “changeScene” to assign it. Oh, and one other thing… you have to pass on your draw calls from the sketch! This is just a matter of doing “sceneManager.draw( g )”. Here is a snippet to add a scene to the manager:

TitleScene titleScene = new TitleScene(); // Your implmentation
sceneManager.addScene( "Title", titleScene );

And Voila! Instant scene management!

1 Comment

  1. Scene Management Article « Jake Seigel's Blog said,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: