3
\$\begingroup\$

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?

\$\endgroup\$
3

1 Answer 1

2
\$\begingroup\$

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);
   }
}
\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – Chris
    Oct 1, 2010 at 18:23
  • \$\begingroup\$ 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. \$\endgroup\$
    – Chris
    Oct 1, 2010 at 18:34
  • \$\begingroup\$ 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.) \$\endgroup\$ Oct 3, 2010 at 10:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .