Davlog
Davlog

Reputation: 2238

Android Game Developement Several scenes

I am building a game for android using Canvas for 2D Graphics. The way I did it so far is I have a GameView (SurfaceView) which has a GameLoop that calls its onDraw() method.

Now for each Scene I extended the GameView and for the MainMenu for example I draw all the Buttons & Backgrounds. When I click a Button I will determine what to do next in the MainScene (extending GameView) onTouchEvent. Let's say I click the Settings Button, then I will start a new Activity which contains only a new Scene (another Scene extending the GameView).

So for each Scene I have a new Activity just containing an Extension of the GameView which includes a GameLoop to draw 20/30 times per second. And in the onTouchEvents I will either start a new activity / finish an activity or do something else.

MainActivity (MainScene ) -> SettingsActivity(SettingsScene) or StoreActivity(StoreScene) etc. Is that a correct way to do it? Or is it inefficient?

I couldn't find tutorials or a lot to read about multi scene game applications using canvas.

Upvotes: 2

Views: 123

Answers (1)

Stepango
Stepango

Reputation: 4851

Actually launch new Activity for each scene is OK when you are developing standard Android apps. It would be ok for simple games or when you don't want to manually manage resources(Activity will be destroyed when system needs resources and will be recreated when needed). In this approach, you don't have full control on Scene transitions and you'll need to reinit some things for each activity that could be inited once for the entire game.

If you want to make scene transitions a faster and more efficient you have at least 3 more variants.

  1. Fragments. Instead off launching activity for every scene you could replace Fragments that contains a view of your scene. In this approach you have more control on transitions and fragments state.

  2. Views. Instead of replacement fragments you could replace views in the same way, using Frame layout as container for example.

  3. Scene drawers. Instead of replacing views you could just replace Drawer class in your view. For example, you could create an interface Scene that contains method draw(Canvas, args...) and make all your scenes implement it. After that you could just replace scene you want to draw inside View.

Any way i prefer to use http://libgdx.badlogicgames.com/ for simple android games. Main benefit is you can run your game on your desktop to test instead of waiting for deployment on device.

Upvotes: 1

Related Questions