AndEngine Tutorial 06 – Platform – 01
Posted by Vexillum on November 11, 2012In the 6th AndEngine tutorial we will learn how to create platforms and check when our player collides with them.
Let’s get started
Just like when we created “Player.java” (AndEngine Tutorial 03 – Player Movement 01), we now have to create a new class for the platforms called “Platform.java”, into com.PerleDevelopment.AndEngine.tutorial.objects, and extend the class by “GameObject”.
Now Eclipse wants us to define a constructor, the quick fix Eclipse suggests is not the constructor we want. We want:
public Platform(int pX, int pY, TextureRegion pTextureRegion, VertexBufferObjectManager pVertexBufferObjectManager) { super(pX, pY, pTextureRegion, pVertexBufferObjectManager); }
Some Information about the code:
Platform( pX, pY, pTextureRegion, pVertexBufferObjectManager)
- pX & pY
- determines the position of the Platform
- pTextureRegion
- we are going to load a “TextureRegion”
- pVertexBufferObjectManager
- this is some OpenGL stuff http://www.opengl.org/wiki/Vertex_Buffer_Object
- just input “getVertexBufferObjectManager()”
After we added the constructor Ecplise also wants us to implement the “move()” method.
Just add this method using “quick fix”.
Your “Platform.java” should look something like this:
Platform.java
package com.PerleDevelopment.AndEngine.tutorial.objects; import org.andengine.opengl.texture.region.TextureRegion; import org.andengine.opengl.vbo.VertexBufferObjectManager; public class Platform extends GameObject { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public Platform(int pX, int pY, TextureRegion pTextureRegion, VertexBufferObjectManager pVertexBufferObjectManager) { super(pX, pY, pTextureRegion, pVertexBufferObjectManager); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void move() { // TODO Auto-generated method stub } // =========================================================== // Methods // =========================================================== }
Now that we created the platform class we have to go back to “Player.java” to check whether the player is colliding with the platform.
In “Player.java” we create an “ArrayList” which later should contain all the platforms.
private ArrayList mPlatforms;
I order to access the private variable “mPlatforms”, we can Eclipse generate Getters and Setters or just copy this code:
public ArrayList getmPlatforms() { return mPlatforms; } public void setmPlatforms(ArrayList mPlatforms) { this.mPlatforms = mPlatforms; }
To check whether the player is colliding or not we can use the “collidesWith(IShape pOtherShape)” method.
Just add the code, which checks if the player collides with a platform, below into the move method.
for (Platform Platform : getmPlatforms()) { if (this.collidesWith(Platform)) { Log.v("objects.Player", "just collided with platform ;)"); } }
Your “Player.java” should look something like this:
Player.java
package com.PerleDevelopment.AndEngine.tutorial.objects; import java.util.ArrayList; import org.andengine.opengl.texture.region.TextureRegion; import org.andengine.opengl.vbo.VertexBufferObjectManager; import android.util.Log; 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; private ArrayListmPlatforms; // =========================================================== // Constructors // =========================================================== public Player(final float pX, final float pY, final TextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) { super(pX, pY, pTextureRegion, pVertexBufferObjectManager); } // =========================================================== // Getter & Setter // =========================================================== public ArrayList getmPlatforms() { return mPlatforms; } public void setmPlatforms(ArrayList mPlatforms) { this.mPlatforms = mPlatforms; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void move() { this.mPhysicsHandler.setVelocityX(-AccelerometerHelper.TILT * DEFAULT_VELOCITY); setRotation(-AccelerometerHelper.TILT * 7); OutOfScreenX(); Jumping(); for (Platform Platform : getmPlatforms()) { if (this.collidesWith(Platform)) { Log.v("objects.Player", "just collided with platform ;)"); } } } // =========================================================== // Methods // =========================================================== private void Jumping() { if (jumping) { Jump(); } else { Fall(); } } private void Jump() { if (mY <= AndEngineTutorialActivity.CAMERA_HEIGHT / 2) { // mY <= 400 jumping = false; } else { this.mPhysicsHandler.setVelocityY(-DEFAULT_VELOCITY); } } private void Fall() { 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; } } }
The last thing we have to do is loading the texture, creating the platforms and adding them into the game.
For this we have to go back into “AndEngineTutorialActivity.java”, and create a new ArrayList
private ArrayList mPlatforms = new ArrayList();
Next thing to do it loading the texture (https://raw.github.com/Perle-Development/AndEngine-Tutorial/master/assets/platform.png), just like we did in “AndEngine Tutorial 02 – Textures and Sprites” (since your texture is 64p*32p, we have to make your TextureAtlas 64p*64p).
/* Texture for platform */ this.mPlatformBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 64); this.mPlatformTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mPlatformBitmapTextureAtlas, this, "platform.png", 0, 0); this.mPlatformBitmapTextureAtlas.load();
Don’t forget to create the BitmapTextureAtlas and the TextureRegion.
In order to add the platforms into the game copy this code into the onCreateScene.method.
mPlatforms.add(new Platform(100, 500, this.mPlatformTextureRegion, this.getVertexBufferObjectManager())); mPlatforms.add(new Platform(200, 600, this.mPlatformTextureRegion, this.getVertexBufferObjectManager())); mPlatforms.add(new Platform(300, 700, this.mPlatformTextureRegion, this.getVertexBufferObjectManager())); oPlayer.setmPlatforms(mPlatforms); // Adds platforms, so we can work with them (Player.java) for (Platform Platform : mPlatforms) { this.mMainScene.attachChild(Platform); }
Platforms.add(new Platform(pX, pY, pTextureRegion, pVertexBufferObjectManager));
- pX & pY
- determines the position of the platform
- pTextureRegion
- This tells AndEngien which texture to load
- pVertexBufferObjectManager
- this is some OpenGL stuff http://www.opengl.org/wiki/Vertex_Buffer_Object
- just input “getVertexBufferObjectManager()”
oPlayer.setmPlatforms(mPlatforms);
- Send the new created Platforms to “Player.java”, so we can check whether the player collides with them or not.
for (Platform Platform : mPlatforms) {this.mMainScene.attachChild(Platform);}
- A simple “For-Each Loop” which adds every platform to the scene.
where is method for Fall()?
I’ve tried your code, and method Jump() never got checked because the condition inside method Jump(). So, jumping is always false.
(sorry for my bad explanation and my english)
😀 thanks, I fixed it
You can always check the code on github.
https://github.com/Perle-Development/AndEngine-Tutorial/blob/master/src/com/PerleDevelopment/AndEngine/tutorial/objects/Player.java
eclipse gives me an error ..
for (Platform Platform : mPlatforms) {
this.mMainScene.attachChild(Platform);
}
eclipse highlights mPlatforms
and says
“type mismatch: cannot convert from element type Object to Platform”
any suggestions or insights?
Thx 4 ur nice work. Very useful for every beginner.
A Box2D AndEngine Tutorial would be very nice.
Good article but how about a code highlight? It would be better to read the code.
Extremely helpful tutorial. I wish i had found this earlier. I am working on a simple doodle like jumping game and i have everything working except when the jumping sprite goes off the top of the screen. Could you provide me some tips?
This is a good tutorial. I have created tutorials for andengine. You can refer them on http://www.nerd yguru.com . I am planning for full game tutorial very soon. You can refer http://www.nerd yguru.com tutorials for creating framework for the games. Once the framework is created you can resuse it for different games.
Hi, I just wanted to state that there are some problems with the style you are using for your blog. The code and some of the text runs off the right side of the page, making it difficult to follow your tutorial.
I have disabled the style sheet to view it, but I thought you may want to know.
P.S. thanks for the great content!
Is there no 7th part?
Very good tutorials BTW 😀