AndEngine Tutorial 03 – Player Movement 01

Posted by Vexillum on June 06, 2012

In the 3rd AndEngine tutorial we will clean up our project a little bit and we will learn how to add a force to our player.

Clean up

I chanced a few variable names in our project, just so they match the AndEngine examples.

  • mPlayerTextureRegion to mPlayerTiledTextureRegion
    • We are going to change it to a TiledTextureRegion
  • iStartX to centerX
  • iStartY to centerY

Let’s get started

Now to be able to move our Player we could use the “AndEnginePhysicsBox2DExtension” but since we just want it to move left and right und jump I think it’s easier without the extension.

Now create a new class called “GameObject” and move it to “com.PerleDevelopment.AndEngine.tutorial.objects” just so our project is kept organized. Just copy and paste the code into your class.

package com.PerleDevelopment.AndEngine.tutorial.objects;

import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

public abstract class GameObject extends AnimatedSprite {

	// ===========================================================
	// Constants
	// ===========================================================

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

	public PhysicsHandler mPhysicsHandler;

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

	public GameObject(final float pX, final float pY, final ITiledTextureRegion pTiledTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
		super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
		this.mPhysicsHandler = new PhysicsHandler(this);
		this.registerUpdateHandler(this.mPhysicsHandler);
	}

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

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

	@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
		move();

		super.onManagedUpdate(pSecondsElapsed);
	}

	// ===========================================================
	// Methods
	// ===========================================================

	public abstract void move();
}


Some Information about the code:

GameObject has to be abstract since we created a new abstract method (move).

  • GameObject (just hands over the vales to AnimatedSprite)
    • AnimatedSprite(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager)
      • pX & pY
        • See Tutorial 2
      • pTiledTextureRegion
        • See Tutorial 2
      • pVertexBufferObjectManager
        • See Tutorial 2

We then create a new PhysicsHandler and register it.

Since GameObject is extended by AnimatedSprite we get the method onManagedUpdate, inside which we call the method move.

Now we finish the “GameObject” class we need to create another class (Player) which hands over the values to “GameObject”.

So just create the new class called “Player” and mote it to “com.PerleDevelopment.AndEngine.tutorial.objects” and copy and paste the code into your class.

package com.PerleDevelopment.AndEngine.tutorial.objects;

import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.PerleDevelopment.AndEngine.tutorial.AndEngineTutorialActivity;

public class Player extends GameObject {

	// ===========================================================
	// Constants
	// ===========================================================

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

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

	public Player(final float pX, final float pY, final TiledTextureRegion pTiledTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
		super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
	}

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

	@Override
	public void move() {
		this.mPhysicsHandler.setVelocityX(100);
	}

	// ===========================================================
	// Methods
	// ===========================================================

}


Some Information about the code:

The Player class is extended by GameObject since we hand the values over to GameObject.

  • Player(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager)
    • pX & pY
      • See Tutorial 2
    • pTiledTextureRegion
      • See Tutorial 2
    • pVertexBufferObjectManager
      • See Tutorial 2

Now to add a force to the Player we just set a velocity to the PhysicsHandler.

We created all necessary classes but we haven’t linked them to our Player yet.

Go back to “AndEngineTutorialActivity” and change

final Sprite oPlayer = new Sprite(iStartX, iStartY, mPlayerTextureRegion, getVertexBufferObjectManager());


to

final Player oPlayer = new Player(centerX, centerY, this.mPlayerTiledTextureRegion, this.getVertexBufferObjectManager());

since the class “Player” needs TiledTextureRegion we have to change our TextureRegion to a TiledTextureRegion.

Your “AndEngineTutorialActivity” 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.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import com.PerleDevelopment.AndEngine.tutorial.objects.Player;

public class AndEngineTutorialActivity extends SimpleBaseGameActivity {

	// ===========================================================
	// Constants
	// ===========================================================

	public static final int CAMERA_WIDTH = 480;
	public static final int CAMERA_HEIGHT = 800;

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

	private Camera mCamera;
	private Scene mMainScene;

	private BitmapTextureAtlas mBitmapTextureAtlas;
	private TiledTextureRegion mPlayerTiledTextureRegion;

	// ===========================================================
	// 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() {
		// Load all the textures this game needs.
		this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32);
		this.mPlayerTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0, 1, 1);
		this.mBitmapTextureAtlas.load();
	}

	@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));

		// Centre the player on the camera.
		final float centerX = (CAMERA_WIDTH - this.mPlayerTiledTextureRegion.getWidth()) / 2;
		final float centerY = (CAMERA_HEIGHT - this.mPlayerTiledTextureRegion.getHeight()) / 2;

		// Create the sprite and add it to the scene.
		final Player oPlayer = new Player(centerX, centerY, this.mPlayerTiledTextureRegion, this.getVertexBufferObjectManager());
		this.mMainScene.attachChild(oPlayer);

		return this.mMainScene;
	}

}


Just run the application to see what happens…

The Player moves to the right and then leaves the screen. To fix this we can add a new method (OutOfScreenX()) to “Player.java” which moves the Player on the left side of the screen when it hits the right side of the screen.
We also need to call your new method under move().

private void OutOfScreenX() {
	if (mX > AndEngineTutorialActivity.CAMERA_WIDTH) { // OutOfScreenX (right)
		mX = 0;
	} else if (mX < 0) { // OutOfScreenX (left)
		mX = AndEngineTutorialActivity.CAMERA_WIDTH;
	}
}


mX returns the current position (x value from the left side of the screen) of the Player










Your Classes should like something like this:

AndEngineTutorialActivity

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.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import com.PerleDevelopment.AndEngine.tutorial.objects.Player;

public class AndEngineTutorialActivity extends SimpleBaseGameActivity {

	// ===========================================================
	// Constants
	// ===========================================================

	public static final int CAMERA_WIDTH = 480;
	public static final int CAMERA_HEIGHT = 800;

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

	private Camera mCamera;
	private Scene mMainScene;

	private BitmapTextureAtlas mBitmapTextureAtlas;
	private TiledTextureRegion mPlayerTiledTextureRegion;

	// ===========================================================
	// 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() {
		// Load all the textures this game needs.
		this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32);
		this.mPlayerTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0, 1, 1);
		this.mBitmapTextureAtlas.load();
	}

	@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));

		// Centre the player on the camera.
		final float centerX = (CAMERA_WIDTH - this.mPlayerTiledTextureRegion.getWidth()) / 2;
		final float centerY = (CAMERA_HEIGHT - this.mPlayerTiledTextureRegion.getHeight()) / 2;

		// Create the sprite and add it to the scene.
		final Player oPlayer = new Player(centerX, centerY, this.mPlayerTiledTextureRegion, this.getVertexBufferObjectManager());
		this.mMainScene.attachChild(oPlayer);

		return this.mMainScene;
	}

}


Player

package com.PerleDevelopment.AndEngine.tutorial.objects;

import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.PerleDevelopment.AndEngine.tutorial.AndEngineTutorialActivity;

public class Player extends GameObject {

	// ===========================================================
	// Constants
	// ===========================================================

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

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

	public Player(final float pX, final float pY, final TiledTextureRegion pTiledTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
		super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
	}

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

	@Override
	public void move() {

		this.mPhysicsHandler.setVelocityX(100);

		OutOfScreenX();
	}

	// ===========================================================
	// Methods
	// ===========================================================

	private void OutOfScreenX() {
		if (mX > AndEngineTutorialActivity.CAMERA_WIDTH) { // OutOfScreenX (right)
			mX = 0;
		} else if (mX < 0) { // OutOfScreenX (left)
			mX = AndEngineTutorialActivity.CAMERA_WIDTH;
		}
	}
}


GameObject

package com.PerleDevelopment.AndEngine.tutorial.objects;

import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

public abstract class GameObject extends AnimatedSprite {

	// ===========================================================
	// Constants
	// ===========================================================

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

	public PhysicsHandler mPhysicsHandler;

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

	public GameObject(final float pX, final float pY, final ITiledTextureRegion pTiledTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
		super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
		this.mPhysicsHandler = new PhysicsHandler(this);
		this.registerUpdateHandler(this.mPhysicsHandler);
	}

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

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

	@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
		move();

		super.onManagedUpdate(pSecondsElapsed);
	}

	// ===========================================================
	// Methods
	// ===========================================================

	public abstract void move();
}


Download Source Code...