Browse through more Android tutorials. If you'd like to see a tutorial on any particular topic, do leave a comment in the wishlist page. We frequently post new tutorials along with app releases. You may subscribe to our newsletter to get all updates in your inbox.
Now you can get the latest Java source bundled with each app update. Install the app from Google Play and go to Settings > Extras.

«  Create a SMS app Create a notepad/to-do list app  »

Create a game like Flappy Bird in Android using AndEngine


Keywords: AndEngine AndEnginePhysicsBox2DExtension SimpleBaseGameActivity ResourceManager SceneManager MenuScene CameraScene AutoParallaxBackground AnimatedSprite DynamicSpriteBatch TiledSprite Sound Music HUD Font PhysicsHandler GenericPool PhysicsWorld Fixture Body ContactListener


4 « Prev Page

12. Scene Manager

The SceneManager class is designed to be a singleton so that it can manage all scenes in the game. Here is a basic outline of the class.

	public class SceneManager {
		private static final SceneManager INSTANCE = new SceneManager();
		public enum SceneType {SCENE_SPLASH, SCENE_MENU, SCENE_GAME}
		private BaseScene mSplashScene;
		private BaseScene mMenuScene;
		private BaseScene mGameScene;
		private SceneType mCurrentSceneType;
		private BaseScene mCurrentScene;
		private SceneManager() {}
		public static SceneManager getInstance() {
			return INSTANCE;
		public void setScene(SceneType sceneType) {
			switch (sceneType) {
			case SCENE_MENU:
			case SCENE_GAME:
		private void setScene(BaseScene scene) {
			mCurrentScene = scene;
			mCurrentSceneType = scene.getSceneType();

		public SceneType getCurrentSceneType() {
			return mCurrentSceneType;
		public BaseScene getCurrentScene() {
			return mCurrentScene;
		public BaseScene createSplashScene() {
			//TODO implement
			return mSplashScene;
		private BaseScene createMenuScene() {
			//TODO implement
			return mMenuScene;
		private BaseScene createGameScene() {
			//TODO implement
			return mGameScene;

There is a create method for each scene. The setScene() method calls a specific create method corresponding to a scene.
We have to implement the empty create methods to return an instance of Scene. Ideally, the scene instances should be created once and reset before reuse. However, for simplicity we choose to recreate the instance every time a scene is set.

13. Base Scene

BaseScene will be our abstract super class for all scenes. First, create a package com.appsrox.flappychick.scene for holding all scenes in the game.
Create a class inheriting from Scene and define few member variables that are commonly used.
	public abstract class BaseScene extends Scene {
		protected final int SCREEN_WIDTH = GameActivity.CAMERA_WIDTH;
		protected final int SCREEN_HEIGHT = GameActivity.CAMERA_HEIGHT;	
		protected GameActivity mActivity;
		protected Engine mEngine;
		protected Camera mCamera;
		protected VertexBufferObjectManager mVertexBufferObjectManager;
		protected ResourceManager mResourceManager;
		protected SceneManager mSceneManager;

		public BaseScene() {
			mResourceManager = ResourceManager.getInstance();
			mActivity = mResourceManager.mActivity;
			mVertexBufferObjectManager = mActivity.getVertexBufferObjectManager();
			mEngine = mActivity.getEngine();
			mCamera = mEngine.getCamera();
			mSceneManager = SceneManager.getInstance();
		public abstract void createScene();
		public abstract void onBackKeyPressed();
		public abstract SceneType getSceneType();
		public abstract void disposeScene();    
The abstract methods must be implemented by the sub types. Most of these methods would be invoked in SceneManager but onBackKeyPressed() needs to be tied to GameActivity for back button behaviour in a particular scene.
Add this code in GameActivity class.
	public void onBackPressed() {
		if (mSceneManager.getCurrentScene() != null) {

14. Splash Scene

Splash scene Splash scene displays a single image and a text at bottom. Recall that we've already loaded the splash resources in GameActivity. Let's see how a simple scene is created in AndEngine.
Create a class inheriting from BaseScene and implement the abstract methods.
	public class SplashScene extends BaseScene {
		public void createScene() {

			Sprite splash = new Sprite(0, 0, mResourceManager.mSplashTextureRegion, mVertexBufferObjectManager) {
				protected void preDraw(GLState pGLState, Camera pCamera) 
				   super.preDraw(pGLState, pCamera);
			Text copyrightText = new Text(0, 0, mResourceManager.mFont1, "(c) 2011-2014", new TextOptions(HorizontalAlign.LEFT), mVertexBufferObjectManager);
			copyrightText.setPosition(SCREEN_WIDTH - copyrightText.getWidth()-5, SCREEN_HEIGHT - copyrightText.getHeight()-5);
		public void onBackKeyPressed() {

		public SceneType getSceneType() {
			return SceneType.SCENE_SPLASH;

		public void disposeScene() {

The important piece of code is in createScene() method where we create a sprite and text, and attach as child to the scene. Notice the use of Font resource in creating the Text object.
Sprite and Text are sub types of Entity. We need to specify position of an entity in a scene. Since we are using GLES2 branch hence origin is at the left-top of screen.

If you choose to use GLES2-AnchorCenter branch then remember that origin is in the lower left.

Share the love:  

Next Page » 4

App Gen
App Name:
Project Name:
List  Grid  Pager
Action Bar
Navigation Drawer
Dummy Data
Free Apps