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.

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
2  
That you are on Android really does not matter. I feel like everything high-level about game state is adequately covered by gamedev.stackexchange.com/questions/1783/game-state-stack and gamedev.stackexchange.com/questions/4131/…. –  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
1  
Yes we have. gamedev.stackexchange.com/questions/651/… –  user744 Oct 1 '10 at 9:01

1 Answer 1

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) {
      input.Update();
      sim.Process(input);
      sim.Render(renderer);
   }
}
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: droidnova.com/playing-with-graphics-in-android-part-i,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

 
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.