AndEngine Tutorial 01 – Creating a Scene

Posted by Vexillum on May 05, 2012

AndEngine a “Free (open-source) Android 2D OpenGL Game Engine” which was first released in mid-2010 by Nicolas Gramlich. Now, 2 years later a lot has changed and AndEngine became one of the most popular 2D game engines for android.

In the next view tutorials I’m trying to teach you how to use AndEngine to make a “DoodleJump”-like game.

If you haven’t downloaded and installed the latest version of AndEngine please read through these tutorials…

 

Creating an Android Project and adding the AndEngine Library

AndEngine Tutorial 01 – Creating a Scene – Create Project

Select New->Other->”Android Project”, give it a project name e.g. “AndEngine Tutorial” and select “android 2.2″ as the build target. In the last step we need to give it a package name e.g. “com.PerleDevelopment.AndEngine.tutorial”.

 

AndEngine Tutorial 01 – Creating a Scene – AndEngine Library
To add AndEngine as a library select Properties->”Add…”->”AndEngine” .

Creating a Scene

To start programming we need to modify “AndEngineTutorialActivity.java”. Just copy and paste the code into “AndEngineTutorialActivity.java” replacing the old code.

This is the simplest version of a “SimpleBaseGameActivity”. It only loads a scene without a background color or any sprites.

package com.PerleDevelopment.AndEngine.tutorial;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.util.FPSLogger;
import org.andengine.ui.activity.SimpleBaseGameActivity;

public class AndEngineTutorialActivity extends SimpleBaseGameActivity {
	// ===========================================================
	// Constants
	// ===========================================================
	static final int CAMERA_WIDTH = 480;
	static final int CAMERA_HEIGHT = 800;

	// ===========================================================
	// Fields
	// ===========================================================

	private Camera mCamera;
	private Scene mMainScene;

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public EngineOptions onCreateEngineOptions() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
	}

	@Override
	protected void onCreateResources() {
		// TODO Auto-generated method stub

	}

	@Override
	protected Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger()); // logs the frame rate
		this.mMainScene = new Scene();

		return this.mMainScene;
	}

}

 

Now some information about the code:

  • We declare “AndEngineTutorialActivity” to be a subclass of “SimpleBaseGameActivity”, which supports us with three methods which we will override later.
  • Next we set the “CAMERA_WIDTH” and “CAMERA_HEIGHT”. If you know the width and height of you smartphone you can choose them as well. (In a later tutorial we will learn how to get this information from your phone).
  • We then define some fields for the camera and the scene.
  • Now let’s talk about the three methods “SimpleBaseGameActivity” provides us with.
    • onCreateEngineOptions()
      • called when the gam engine is loaded
      • initialize Camera
        • Camera(pX,pY, pWidth, pHeight);
          • pX & pYare the coordinates for the origin of the camera
          • pWidth & pHeight are the dimensions, in pixels, of the camera
      • initialize Energie
        • EngineOptions(pFullscreen, pScreenOrientation, pResolutionPolicy(pWidth, pHeight), pCamera)
          • pFullscreen determines whether the game will be play full screen or not
          • pScreenOrientation, here we can choose between “LANDSCAPE” and “PORTRAIT”
          • pResolutionPolicy is the ratio of you Engine(same values as in Camera)
        • pCamera is the camera object
    • onCreateResources()
      • call to load resources
    • onCreateScene()
      • call when everything is setup and all the texture are loaded
      • this.mEngine.registerUpdateHandler(new FPSLogger());
        • logs the frame rate
      • this.mMainScene = new Scene();
        • creates the scene

Just to have something to play with add this line of code to onCreateScene() just after the scene is created.

You can of course change the background color.

You code should look something like this:

package com.PerleDevelopment.AndEngine.tutorial;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.util.FPSLogger;
import org.andengine.ui.activity.SimpleBaseGameActivity;

public class AndEngineTutorialActivity extends SimpleBaseGameActivity {
	// ===========================================================
	// Constants
	// ===========================================================
	static final int CAMERA_WIDTH = 480;
	static final int CAMERA_HEIGHT = 800;

	// ===========================================================
	// Fields
	// ===========================================================

	private Camera mCamera;
	private Scene mMainScene;

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public EngineOptions onCreateEngineOptions() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
	}

	@Override
	protected void onCreateResources() {
		// TODO Auto-generated method stub

	}

	@Override
	protected Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger()); // logs the frame rate

		/* Create Scene and set background colour to (1, 1, 1) = white */
		this.mMainScene = new Scene();
		this.mMainScene.setBackground(new Background(1, 1, 1));

		return this.mMainScene;
	}

}