AndEngine Tutorial 03 – Player Movement 01
Posted by Vexillum on June 06, 2012In 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(); }
Yo man, I really appreciate these tutorials. They are by far the best written tutorials for AndEngine I have come across. Keep up the good work! I wish other people wrote tutorials as well as you do 😛
yeah, agreed with Cheese.
the BEST
Very good tutorials. Hope you soon make more of these as i am new to andengine and myself trying to learn. Thanks!
Well done. I can’t wait to try this stuff.
how to get velocity and vector of player that is moveable by a user?
Good stuff!! setVelocityX moves it x wise. If I set both setVelocityX and setVelocityY, will it move diagonally? Probably it will, let me try out! Great tutorial, bro!
Thanks for your tutorials!
It is very helpful especially how you explain each method and parameter.
Waiting for your next tutorials!
You are awesome!!
Everything is so precise and clear, I can’t imagine how you do this!!
a part of changing this line:
final Sprite oPlayer = new Sprite(iStartX, iStartY, mPlayerTextureRegion, getVertexBufferObjectManager());
you have to change this other one:
In Tutorial 02 the line is like this:
this.mPlayerTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, “face_box.png”, 0, 0, 1, 1);
and in this Tutorial it apear like this:
this.mPlayerTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, “face_box.png”, 0, 0, 1, 1);
note the diference is “. createTiledFromAsset(” in tutorial 02 apear “.createFromAsset () “.
GOOD JOB.
Nice work. Which WordPress plugin are you using to display the source code? It looks really clean.
Thanks for your comment.
I’m using SyntaxHighlighter Evolved (http://wordpress.org/extend/plugins/syntaxhighlighter/)
Thanks a lot for making these tutorials man, they’re really helpful! 😀
Why do you use ITiledTextureRegion in some places, but TiledTextureRegion in others?
Also, an observation. What you put in the method move will be executed for every frame. So, this.mPhysicsHandler.setVelocityX(100); shouldn’t be there, but in the constructor. On the other hand, it’s correct that OutOfScreenX(); is in move, because you need to check it for every frame.
Nice tutorial, but i don’t understand where is mX variable. i know if It’s in Entity class, but i don’t know which
Can you explain it? tnx 
relationship there are
Hey,
to be ecaxt it is “org.andengine.entity.Entity.mX”.
mX returns the current x-position of the Entity
Dude, you just made my day! Awesome tutorials!!! It is so hard to find documentation. Please keep it up!!!
Thank you!!!!
I was using movemodifiers for my sprite movement , while you used physics handler and setting velocities for them in onManagedUpdate() … I think your approach is better but is there any problem with my approach .. assume i have a car sprite racing along a road , is it preferable to use physics handler or modifiers?
Thanks man, awesome tutorials!
Hey Vex,
Thanks for the tutorial.
I’ve been following along for a couple of days, and already I’ve got my own Android game up and running.
These are by FAR the best tutorials I’ve seen in the web.
-Emilio
dude i cant even see the example code.
oh it shows up now, my internet fucked up 😀
But seriously why dont you tell us what mX stands for?
Where is libs folder?? and andegine.jar ı dont know which you andegıne
You don’t need a jar….
Please read these two turoials:
http://perle-development.com/tutorials/setup-eclipse-part-2/
http://perle-development.com/tutorials/andengine-tutorial-01-creating-a-scene/
where can ı find andegine.jar for this tutorial?
Its really valuable tutorials.But I don’t understand one thing that if I want to add more players and want to move them to Y-Axis, then what to do. I have to create more GameObject and Player classes for other players OR I can use these two classes for other players. This question really confusing me now.