Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

All

I've have an interface that allows me to switch between different scenes in my Android game. When the home key is pressed, I am saving all of my states (score, sprite positions etc) into a Bundle. When re-launching, I am restoring all my states and all is OK - however, I can't figure out how to save my 'Scene', thus when I return, it always starts at the default screen which is the 'Main Menu'.

How would I go about saving my 'Scene' (into a Bundle)?

Code

import android.view.MotionEvent;

public interface Scene{

void render();
void updateLogic();
boolean onTouchEvent(MotionEvent event);

}

I assume the interface is the relevant piece of code which is why I've posted that snippet.

I set my scene like so: ('options' is an object of my Options class which extends MainMenu (Another custom class) which, in turn implements the interface 'Scene')

 SceneManager.getInstance().setCurrentScene(options);  //Current scene is optionscreen
share|improve this question
add comment

1 Answer

Use a piece of data to denote your scenes. A simple enumeration is the easy way; even a string works just fine in a pinch.

Your interface should be extended to have something like

ESceneType getSceneType();

which returns the type of that scene (e.g. ESceneType.MainMenu or ESceneType.Pause or whatever).

You then create a factory object which, using either a data set or a hard-coded switch statement or the like, instantiates scene objects based on id. The low-tech hard-coded version is something like

Scene createScene(ESceneType type) {
  switch (type) {
    case ESceneType.MainMenu: return new SceneMainMenu();
    case ESceneType.Pause: return new ScenePause();
    case ESceneType.Play: return new ScenePlay();
    default: throw error;
  }
}

Now you can save this scene type value easily, query it from the active Screne instance, and recreate a scene from it.

share|improve this answer
    
Hi @SeanMiddleditch - thanks for this, I don't quite understand how this works, what exacly is ESceneType? I can currently get my scene type by doing the following: SceneManager.getInstance().getCurrentScene(); I'm don't quite understand how this can be used to save / recall the Scene - could you elaborate please? Thanks! –  user22241 Jun 26 '13 at 17:51
    
"a simple enumeration" - just make an easy to serialize piece of data to identify your scenes and then reconstruct them when loading. I'm assuming your getCurrentScene return an instance of a Scene derived type, not an identifier for the scene. You have to make your own enumeration or string types here, or utilize Java's introspection APIs. If you need any extra per-scene data you'll need to serialize that yourself, too. How to do that is more of a basic programming/Java query not specific to game development. –  Sean Middleditch Jun 26 '13 at 18:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.