Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Currently developing a game for Android. I went through a tutorial to get sprites, loop, etc. up and running, and it all works fine. The way it is currently set up is that Main creates a canvas, the canvas then runs a thread which has the while loop calling update() and display() functions in the canvas class. All of the game code and classes are essentially called from the canvas.

My question is, how do I expand this to include multiple screens/states, like menus, level selection, options, etc?

share|improve this question
That you are on Android really does not matter. I feel like everything high-level about game state is adequately covered by and…. – user744 Oct 1 '10 at 0:03
This question doesn't need the "android" bit, nor "java", but afaik we haven't discussed the toplevel game loop here yet. – dash-tom-bang Oct 1 '10 at 0:18
Yes we have.… – user744 Oct 1 '10 at 9:01

It's probably not appropriate to have Canvas be the controller of everything. You probably want to create a Canvas and hand it to something else to draw into.

The way I'd set it up would be something along these lines:

static public void Main() {
   InputGatherer input = new InputGatherer();
   GameSimulation sim = new GameSimulation();
   Renderer renderer = new Renderer();

   // the renderer will internally create a canvas,
   // which is irrelevant to the outside world.

   boolean done = false;

   while (!done) {
share|improve this answer
ah my mistake. I just looked back at my code. It's a view handling all the game logic. The code you posted looks a lot like my loop, except my loop runs in a thread started by the view, which is set as the cotent view in main. – Chris Oct 1 '10 at 18:23
Here is the tutorial I used:,147.html By step five, he has all logic running on his View. I guess my question is how to expand this out to include multiple states. If I were to take a crack at it right now, I would create a root view in main, which has a subview for each game state, which would each run its own loop when active. I doubt, however that this is the best answer. Sorry, I have done games mostly in Flash before, where switching states was as easy as switching frames. – Chris Oct 1 '10 at 18:34
I haven't done enough Android development to say for sure, but I would suggest that you do not want your Views to be doing any logic. Views are for viewing, they should only be drawing whatever the data model says that they should draw. Likely it's the Activity that should be updating all of this stuff. (Put 'View' in place of 'Renderer' in my example above.) – dash-tom-bang Oct 3 '10 at 10:58

Your Answer


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.