AndEngine Tutorial 05 – Player Movement 03

Posted by Vexillum on September 09, 2012

In the 5th AndEngine tutorial we will add the jump function to our little game.
This is simple, we are just going to create a new Jump method (in Player.java) and let the player jump from the bottom of the screen to the middle of the screen.

Let’s get started

Before we start we need to know how AndEngine handles 2D Coordinate Systems. When you think about it you would think that the bottom left is (0, 0). Well sadly that not true, the Point (0,0) actually is in the left up corner.

2D Coordinate Systems
So just like we did before, we have to give the Player a velocity. Since we want the player to jump up and down it is a velocity in the Y direction (setVelocityY).
Let’s test this. Add “this.mPhysicsHandler.setVelocityY(DEFAULT_VELOCITY);” into the “move()”-method (Player.java). In order to make the player move up we have to set DEFAULT_VELOCITY a negative value (e.g. -100), to let him fall down we have to set DEFAULT_VELOCITY a positive value (e.g. 100).

Before we continue be sure to remove the line (“this.mPhysicsHandler.setVelocityY(DEFAULT_VELOCITY);”) we just added.

private void Jumping() {
	if (jumping) {
		Jump();
	} else {
		Fall();
	}
}

private void Jump() {
	if (mY = AndEngineTutorialActivity.CAMERA_HEIGHT) { // mY >= 800
		jumping = true;
	} else {
		this.mPhysicsHandler.setVelocityY(DEFAULT_VELOCITY);
	}
}

Since we don’t just want the player to move up or down but rather want him to jump, we have to be sure DEFAULT_VELOCITY is positive until the player hits the bottom of the screen and then DEFAULT_VELOCITY has to turn negative so the player moves up again.
This is very similar to our “OutOfScreenX()”-method. We simply use “mY” which gives us the current position of the player (in the Y direction) and check whether the player is still falling towards the bottom or has already reached the bottom.
The initial value of “jumping” is false, so the player will “Fall()” until it hits “AndEngineTutorialActivity.CAMERA_HEIGHT”, then “jumping” will turn true and the player will “Jump()”.
Just add “Jumping();” into the “move()” method and the player is ready to jump up and down.

 

Player.java

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;
import com.PerleDevelopment.AndEngine.tutorial.helper.AccelerometerHelper;

public class Player extends GameObject {

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

	final int DEFAULT_VELOCITY = 200;

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

	boolean jumping = false;

	// ===========================================================
	// 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(-AccelerometerHelper.TILT * DEFAULT_VELOCITY);
		setRotation(-AccelerometerHelper.TILT * 7);
		OutOfScreenX();

		Jumping();

	}

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

	private void Jumping() {
		if (jumping) {
			Jump();
		} else {
			Fall();
		}
	}

	private void Jump() {
		if (mY = AndEngineTutorialActivity.CAMERA_HEIGHT) { // mY >= 800
			jumping = true;
		} else {
			this.mPhysicsHandler.setVelocityY(DEFAULT_VELOCITY);
		}
	}

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