Create a game like Flappy Bird in Android using AndEngine
DownloadKeywords: AndEngine AndEnginePhysicsBox2DExtension SimpleBaseGameActivity ResourceManager SceneManager MenuScene CameraScene AutoParallaxBackground AnimatedSprite DynamicSpriteBatch TiledSprite Sound Music HUD Font PhysicsHandler GenericPool PhysicsWorld Fixture Body ContactListener
Contents- Overview
- Create a new Eclipse Android project
- Add AndEngine library
- Add AndEngine Physics Box2D extension
- Android manifest
- AndEngine concepts
- Game Activity
- Resource Manager
- TextureAtlas & TextureRegion
- Font
- Sound & Music
- Scene Manager
- Base Scene
- Splash Scene
- Main Menu Scene
- Sub Menu Scene
- Game Scene
- Auto Parallax Background
- HUD
- Bird
- Pipes
- Generic Pool
- Physics World
- Body & Fixture
- Contact Listener
- Camera Scene
- What's next?
6. AndEngine concepts
Engine:: The Engine takes care of the game loop and tells the Android OS and openGL how to draw the scene. The speed at which the engine can draw entities to the screen is referred to as frame rate.Resolution Policy: It tells AndEngine how to deal with the different screen-sizes of different devices. e.g. keep everything scaled in in proper ratio (RatioResolutionPolicy) or fill whole resolution (FillResolutionPolicy)
Camera: The camera offers a view of a scene that is drawn on the screen, as the whole scene is not visible all the time. It can zoom, pan, follow a particular sprite, etc.
Scene: Scenes are the containers to which backgrounds, sprites, text, controls, and more are added. There are subclasses, like the CameraScene/HUD/MenuScene that draw to the same position of the Scene irrespective of the camera position.
HUD: HUD is a subclass of CameraScene. It is used for displaying score and game controls (that has to be at the same position and follow camera).
Entity: An Entity is an object that can be drawn on the screen, like Sprites, Rectangles, Text or Lines. An Entity has a position/rotation/scale/color/etc.
Sprite: Is an entity with texture. There are subclasses - TiledSprite is an entity with tiled texture created from a series of images. AnimatedSprite is extension of the TiledSprite, you may animate tiles after specific intervals to give the illusion of movement and action.
Texture: A Texture is a 'image' in the memory of the GPU. It's a graphic used to give an object a detailed appearance.
TextureRegion: A TextureRegion defines a rectangle on the Texture. A TextureRegion is used by Sprites to let the system know what part of the big Texture the Sprite is showing.
Box2D: Box2D is a physics engine that takes care of simulating things like gravity, collisions, friction, and more. It calculates how bodies interact within each frame.
The best way to learn AndEngine is through the official examples available on GitHub.
https://github.com/nicolasgramlich/AndEngineExamples
You'll need to import all AndEngine extensions as well for compiling the examples.
7. Game Activity
SimpleBaseGameActivity is an abstract class in AndEngine and provides a good base for our game activity. Simply extending this class and implementing the abstract methods will create a minimal game activity.SimpleBaseGameActivity is a subclass of BaseGameActivity. You can as well use BaseGameActivity as the base class to get more control on the callbacks.
Create a new GameActivity class in package com.appsrox.flappychick.
public class GameActivity extends SimpleBaseGameActivity { @Override public EngineOptions onCreateEngineOptions() { // TODO Auto-generated method stub return null; } @Override protected void onCreateResources() { // TODO Auto-generated method stub } @Override protected Scene onCreateScene() { // TODO Auto-generated method stub return null; } }All the three methods are invoked by the engine in order for properly setting up the game.
Firstly, onCreateEngineOptions() is invoked that allows us to set up the engine options, for e.g. screen orientation, resolution policy, camera, audio options, etc.
Secondly, onCreateResources() is invoked that allows us to load resources, for e.g. texture, sound, music, font, etc.
Thirdly, onCreateScene() is invoked that allows us to set up the scene.
Let's now take a look at the entire class.
public class GameActivity extends SimpleBaseGameActivity { public static final int CAMERA_WIDTH = 320; public static final int CAMERA_HEIGHT = 480; private Camera mCamera; private ResourceManager mResourceManager; private SceneManager mSceneManager; @Override public EngineOptions onCreateEngineOptions() { mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera); engineOptions.getAudioOptions().setNeedsSound(true).setNeedsMusic(true); return engineOptions; } @Override protected void onCreateResources() { mResourceManager = ResourceManager.getInstance(); mResourceManager.prepare(this); mResourceManager.loadSplashResources(); mSceneManager = SceneManager.getInstance(); } @Override protected Scene onCreateScene() { mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback() { public void onTimePassed(final TimerHandler pTimerHandler) { mEngine.unregisterUpdateHandler(pTimerHandler); mResourceManager.loadGameResources(); mSceneManager.setScene(SceneType.SCENE_MENU); mResourceManager.unloadSplashResources(); } })); return mSceneManager.createSplashScene(); } @Override protected void onDestroy() { super.onDestroy(); System.exit(0); } }We have declared new classes ResourceManager & SceneManager that we will discuss in later sections. These classes were created to manage resources and scenes so we don't end up writing all our code in the activity itself.
In onCreateEngineOptions(), we specify screen orientation as portrait and the resolution policy as ratio so that screen can scale proportionately. Additionally, we request sound and music which will be used later.
In onCreateResources(), we only load resources used by splash scene.
In onCreateScene(), we use scene manager to create splash scene. Additionally, we register a timer handler so the splash scene transitions to menu scene automatically when timer expires.
Note that we load resources before setting menu scene and then unload splash resources that are no longer needed. This helps to keep memory usage low.