b2cloud

13th February 2013

Getting started with AndEngine for Android Game Development

Guides | Tutorial By 4 years ago

I’ve been working on a small android game project in my spare time using AndEngine and I’ve noticed tutorials and guides are fairly sparse, so I thought I’d do a few small blog posts to show how to do a few basic things. In this one I’ll give a quick run through on how to get up and running with the latest version of AndEngine.

As of writing, the latest version of AndEngine can be found on GitHub at here. But first you’re going to need Eclipse set up and ready to go for Android, for which there are many excellent guides such as this one, it’s pretty straight-forward, especially if you don’t already have Eclipse installed- so go do that, and I’ll wait here.

Done? Awesome. Next up you’re gonna want to clone the AndEngine git repo, so you’re going to need Git. If you’re on Mac or Linux just grab it from your repositories, if you’re on Windows try checking out this (If anyone has any better recommendations for Git with Windows please say so :)). When you open up Eclipse it’ll ask you where you want your workspace to be, just clone the AndEngine git repo into there, on Ubuntu for me it looked something like this from the terminal:

 git clone https://github.com/nicolasgramlich/AndEngine.git

“https://github.com/nicolasgramlich/AndEngine.git” is the URL of the repo you want to clone.

Then open up Eclipse in that workspace, and change the Java perspective through:

Window → Open Perspective → Java.

We also want to show the project explorer if it’s not there already, which we can do through:

Window → Show View → Project Explorer.

Now let’s go and import our AndEngine project from earlier:

File → Import

This’ll bring up a dialogue that looks something like this:

Select “Existing Projects into Workspace” and hit next. Now you need to choose the root directory to search for the AndEngine Project so click Browse and navigate to your Workspace. You should now see AndEngine pop up in the space below, tick the box next to it and hit finish.

Now AndEngine should pop up in your project manager. Right click on it and go to Properties, you need to set the Project Build Target to the version with the highest API level you have, at the time of writing AndEngine wanted at least API level 16. Note: This doesn’t mean that AndEngine games won’t run on older devices, it just needs this level to compile. I’ve run games I’ve made on Froyo (2.2) devices even though it needed 4.1+ to compile the AndEngine library. You’ll also need to set your Java Compiler level to 1.6 or 1.5. Check out the shots below for both of these steps.

Hopefully, with all that done, Eclipse should have auto-built your AndEngine JAR file, and you should be good to go. If you look in the bin directory in the project explorer you should see “andengine.jar”.

Now we want to create a new Android Project to test out AndEngine, so go to File -> New Project.

Choose a name and make sure the Minimum required SDK is what you’d like it to be able to run on, and leave the target SDK and Compile with SDK reasonably new. All emulator-based testing will have to be done on ICS at least, as this is the only version of the emulator which supports hardware accelerated graphics.

Hit next and uncheck “Create custom launcher icon”, and hit next again. Just keep hitting next until you can hit finish.

You should be able to get to your MainActivity.java file from the side view now.

 

Now we just want to open that file up and change its super class to SimpleBaseGameActivity.

public class MainActivity extends SimpleBaseGameActivity {

After having done that, we should see a beautiful squiggly red line. This is of course because we haven’t added AndEngine to our project yet. So just copy your andengine.jar file from the bin directory of the AndEngine project and into the lib directory of our new game.

Now you should be able to mouse over “SimpleBaseGameActivity” in the editor and a little popup should come up.

Click on “Import ‘SimpleBaseGameActivity’ (org.andengine.ui.activity)” and that should clear up our little red line problem … and introduce a new one! MainActivity should be underlined now, so mouse over that and choose “Add unimplemented methods” and you should get:

@Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
return null;
}

@Override
protected void onCreateResources() {
// TODO Auto-generated method stub

}

@Override
protected Scene onCreateScene() {
// TODO Auto-generated method stub
return null;
}

For the sake of simplicity, I’ll just give you some code to get a simple red screen going. (Make sure you don’t delete your package line if you’re copying and pasting).

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.ui.activity.SimpleBaseGameActivity;
import android.util.DisplayMetrics;

public class MainActivity extends SimpleBaseGameActivity {

Camera mCamera;
Scene  mScene;

@Override
public EngineOptions onCreateEngineOptions()
{
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width  = metrics.widthPixels;
int height = metrics.heightPixels;
mCamera = new Camera(0, 0, width, height);

return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(width, height), mCamera);
}

@Override
protected Scene onCreateScene() {
mScene = new Scene();
mScene.setBackground(new Background(1, 0, 0));

return mScene;
}

@Override
protected void onCreateResources() {}
}

Now we need an AVD to run this on. Click on the little AVD button in the toolbar of Eclipse () and click “New”. I set mine up like this:

Make sure you have “Use Host GPU” checked, if you don’t you’ll get an error about EGLConfig as it’s a requirement for GLES2 I believe and you’ll have issues if your AVD isn’t set up to use it. An annoying trade off here is that you cannot have Snapshot enabled which means starting up your AVD will be slow every time. 🙁

Now you’ve created your AVD you can click Start and close the AVD window. Once your AVD is up and running press the green play button to get going. Run as an Android application, and away you go. If all went according to plan you should be greeted by a blindingly bright red screen in the AVD.

Oh, and CTRL+F11 changes to Landscape mode. Good luck!

Recommended Posts

Yammer integrations in ReactJS

Post by 4 years ago

I am writing this blog while I am working on a project for our client’s intranet website. The client requires the website has the ability to share, like and write comments in the website through

Got an idea?

We help entrepreneurs, organizations and established brands from around
the country bring ideas to life. We would love to hear from you!