Android Game Development: Starting with AndEngine

Setting up the Environment

In eclipse import project from Git and use the following URI to add the AndEngine library: https://github.com/nicolasgramlich/AndEngine.git

andengine

If there is any error on the AndEngine library, make sure it has a “res” folder, Right-click on the AndEngine library, and choose Properties (the last option). Once the properties open, select “Java Compiler”. Check the box “Enable project specific settings” and in the drop down options of “Compiler compliance level” make sure the right version of java is selected (mine is 1.6). Select the option “Android” and make sure our “Project Build Target” is selected correctly (I use 4.2). Select the “Java Build Path” properties and then go to the “Order and Export” tab, and make sure all of the checkboxes are check.

To allow our game to use the AndEngine library go to the Android properties of our project, and click the “Add…” button within the “Library” section then select the “AndEngine” library added previously. With the properties menu still open, go to the “Java Build Path”>”Order and Export” and check all of the checkboxes.

Install Box2D

Import project using Git, use this URI: https://github.com/nicolasgramlich/AndEnginePhysicsBox2DExtension.git

Create and Setup Project

Create an android project. Add the andengine.jar library from the AndEngine bin folder into your project lib folder.

Create a HelloWorld AngEngine example:
[cc lang=”java”]
package com.artecapps.androidgame2014;

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.sprite.Sprite;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

public class MainActivity extends BaseGameActivity {

Scene scene;

//how far away from the scene the camera is
protected static final int CAMERA_WIDTH = 800;
protected static final int CAMERA_HEIGHT = 480;

BitmapTextureAtlas playerTexture;
ITextureRegion playerTextureRegion;

@Override
public EngineOptions onCreateEngineOptions() {
//SETUP camera
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);//top left corner and bottom right corner

//first parameter define if camera is landscape:
final EngineOptions options = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
return options;
}

/**
* load all the resources images sprites so the game is not slowed.. this one is called automatically but the others need indication
*/
@Override
public void onCreateResources(OnCreateResourcesCallback arg0) throws Exception {
loadGraphics();
arg0.onCreateResourcesFinished(); //call back to indicate we loaded all our resources so go on to create scene
}

private void loadGraphics() {
//set the path where the images are
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(“images/”);

playerTexture = new BitmapTextureAtlas(getTextureManager(), 128, 128); //have to use 2^x and has to be bigger than the size of image
playerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(playerTexture, this, “face.png”, 0, 0); //refer to the top left corner of image in the atles image
playerTexture.load();//we can unload it later on
}

@Override
public void onCreateScene(OnCreateSceneCallback arg0) throws Exception {
this.scene = new Scene();
this.scene.setBackground(new Background(100, 40, 230));
arg0.onCreateSceneFinished(this.scene);//done creating the scene, move to populate scene
}

@Override
public void onPopulateScene(Scene arg0, OnPopulateSceneCallback arg1) throws Exception {
//adding menu items , characters
//need to remember the ratio of the pictures you add, mine is 124×123

final Sprite sPlayer = new Sprite(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2, playerTextureRegion, this.mEngine.getVertexBufferObjectManager());
sPlayer.setRotation(45.0f);
this.scene.attachChild(sPlayer);
arg1.onPopulateSceneFinished();
}

}

[/cc]
References:

  • http://www.mybringback.com/tutorial-series/12714/installing-andengine-tutorial/
  • http://www.andengine.org/

 

Leave a Reply

Your email address will not be published. Required fields are marked *