AndEngine Tutorial 05 – Player Movement 03
Posted by Vexillum on September 09, 2012In 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.

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; } } }
Great! Could you explain collisions in the next tut please? =)
great tutorial hope to see more from you. tryn to lear this.
good !
Awesome one 😀 😀
So, does the player keep jumping up and down without any input?
(Sorry, I don’t have a compiler or android device around)
-thanks
Yes, the player jumps up and down without any input.
You can use the accelerometer to make the player move left and right.
For all interessted people here, i have found a very simple and nice method to implement the jump function:
So, have fun with it and try the differet jump affects by changing the
Ease(…)getInstance() 😉
.
public static void jump()
{
final float jumpDuration = 0.8f;
final int jumpHeight = 80;
final float startY = Main.player.getY();
final float peakY = startY – jumpHeight;
SequenceEntityModifier jumpModifier = new SequenceEntityModifier(
new MoveYModifier(jumpDuration, startY, peakY, EaseQuadOut.getInstance()),
new MoveYModifier(jumpDuration, peakY, startY, EaseBounceOut.getInstance()));
Main.player.registerEntityModifier(jumpModifier);
}
http://pastebin.com/iRSzrn8A
There is no fall method?
where is the method Fall() ; ?
i have errors because i dont have the fall method
From the full code on github:
https://github.com/Perle-Development/AndEngine-Tutorial/downloads
private void Fall() {
if (mY >= AndEngineTutorialActivity.CAMERA_HEIGHT) { // mY >= 800
jumping = true;
} else {
this.mPhysicsHandler.setVelocityY(DEFAULT_VELOCITY);
}
}
Note also, full code has a correction for jump:
private void Fall() {
if (mY >= AndEngineTutorialActivity.CAMERA_HEIGHT) { // mY >= 800
jumping = true;
} else {
this.mPhysicsHandler.setVelocityY(DEFAULT_VELOCITY);
}
}