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.

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!

share|improve this question

2 Answers 2

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

share|improve this answer
1  
This is the right way to switch activities. For bonus points, you can add transitions (like fade-in, fade-out) with overridePendingTransition(): developer.android.com/reference/android/app/Activity.html –  Steve Blackwell Apr 5 '12 at 13:06
    
Additionally if you want to know more about using View in different thread, I suggest you to read my blog about it - abhishek347.wordpress.com/2012/03/27/asynctask-on-rescue –  noob Apr 5 '12 at 13:09

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.

share|improve this answer

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.