4
\$\begingroup\$

I am trying to create a simple game using the Android SDK without using AndEngine (or any other game engine). I have plenty of experience designing games from the past, but I'm having lots of trouble trying to use the Android SDK to make my game. By far my biggest hurdle right now is switching between views. That is, for example, going from the menu to the first level, etc.

I am using a traditional model I learned (I think it's called a scene stack or something?) in which you push the current scene onto a stack and the game's main loop runs the top item of the stack. This model seems non-trivial to implement in the Android SDK, mostly because Android seems to be picky about which thread instantiates which view. My issue is that I want the first level to show up when you press a button on the main menu, but when I instantiate the first level (the level class extends SurfaceView and implements SurfaceHolder.Callback) I get a runtime error complaining that the thread that runs the main menu can't instantiate this class. Something about calling Looper.prepare(). I figured at this point I was probably doing things wrong.

I'm not sure how to specifically phrase my issue into a question, so maybe I should leave it as either

1) Does anybody know a good way (or the 'proper' way) to switch between scenes in an Android game?

or

2) Are there any tutorials out there which show how to create a game that doesn't take place entirely in one scene? (I have googled for a while to no avail... maybe someone else knows of one?)

Thanks!

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

If you are using Android SDK, then you can use Activity, to switch between Scenes. Every Activity is a different Scene on screen and when one gets launched any previous Activity are sent to Pause status.
Activities can be launched using the following method.

 startActivity(new Intent(this,MyActivity.class));

Learn more about Activity LifeCycle here

\$\endgroup\$
2
-2
\$\begingroup\$

I'm not familiar with the Android SDK, but seeing the error you have it seems that you're somehow not "freeing" you menu when you switch to your level. As Android can't display two of those SurfaceViews at the same time, it squeaks and halts your program. Maybe there's something you have to do on your menu view, some method to call to detach it from the screen, before you create and attach another view? Maybe you should completely destroy your menu before instantiating your level? Maybe you can have only one view for your whole app, so there's no way to switch between views?

I don't know any tutorial either, but my bet is that you'll find some demo in the SDK.

Oh, and if your game has only a few game states, using a scene stack for this might be overkill. A simple FSM is way enough if you just have say a menu, a gameplay state and a credits screen.

\$\endgroup\$

You must log in to answer this question.

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