AndEngine Tutorial 01 – Creating a Scene
Posted by Vexillum on May 05, 2012AndEngine a “Free (open-source) Android 2D OpenGL Game Engine” which was first released in mid-2010 by Nicolas Gramlich. Now, 2 years later a lot has changed and AndEngine became one of the most popular 2D game engines for android.
In the next view tutorials I’m trying to teach you how to use AndEngine to make a “DoodleJump”-like game.
If you haven’t downloaded and installed the latest version of AndEngine please read through these tutorials…
Creating an Android Project and adding the AndEngine Library
Select New->Other->”Android Project”, give it a project name e.g. “AndEngine Tutorial” and select “android 2.2″ as the build target. In the last step we need to give it a package name e.g. “com.PerleDevelopment.AndEngine.tutorial”.
To add AndEngine as a library select Properties->”Add…”->”AndEngine” .
Creating a Scene
To start programming we need to modify “AndEngineTutorialActivity.java”. Just copy and paste the code into “AndEngineTutorialActivity.java” replacing the old code.
This is the simplest version of a “SimpleBaseGameActivity”. It only loads a scene without a background color or any sprites.
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.util.FPSLogger; import org.andengine.ui.activity.SimpleBaseGameActivity; public class AndEngineTutorialActivity extends SimpleBaseGameActivity { // =========================================================== // Constants // =========================================================== static final int CAMERA_WIDTH = 480; static final int CAMERA_HEIGHT = 800; // =========================================================== // Fields // =========================================================== private Camera mCamera; private Scene mMainScene; // =========================================================== // 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() { // TODO Auto-generated method stub } @Override protected Scene onCreateScene() { this.mEngine.registerUpdateHandler(new FPSLogger()); // logs the frame rate this.mMainScene = new Scene(); return this.mMainScene; } }
Now some information about the code:
- We declare “AndEngineTutorialActivity” to be a subclass of “SimpleBaseGameActivity”, which supports us with three methods which we will override later.
- Next we set the “CAMERA_WIDTH” and “CAMERA_HEIGHT”. If you know the width and height of you smartphone you can choose them as well. (In a later tutorial we will learn how to get this information from your phone).
-
We then define some fields for the camera and the scene.
-
Now let’s talk about the three methods “SimpleBaseGameActivity” provides us with.
-
onCreateEngineOptions()
- called when the gam engine is loaded
-
initialize Camera
-
Camera(pX,pY, pWidth, pHeight);
- pX & pYare the coordinates for the origin of the camera
- pWidth & pHeight are the dimensions, in pixels, of the camera
-
-
initialize Energie
-
EngineOptions(pFullscreen, pScreenOrientation, pResolutionPolicy(pWidth, pHeight), pCamera)
- pFullscreen determines whether the game will be play full screen or not
- pScreenOrientation, here we can choose between “LANDSCAPE” and “PORTRAIT”
- pResolutionPolicy is the ratio of you Engine(same values as in Camera)
- pCamera is the camera object
-
-
onCreateResources()
- call to load resources
-
onCreateScene()
- call when everything is setup and all the texture are loaded
-
this.mEngine.registerUpdateHandler(new FPSLogger());
- logs the frame rate
-
this.mMainScene = new Scene();
- creates the scene
-
Just to have something to play with add this line of code to onCreateScene() just after the scene is created.
You can of course change the background color.
You code 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.ui.activity.SimpleBaseGameActivity; public class AndEngineTutorialActivity extends SimpleBaseGameActivity { // =========================================================== // Constants // =========================================================== static final int CAMERA_WIDTH = 480; static final int CAMERA_HEIGHT = 800; // =========================================================== // Fields // =========================================================== private Camera mCamera; private Scene mMainScene; // =========================================================== // 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() { // TODO Auto-generated method stub } @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)); return this.mMainScene; } }
Thanks for writing this tutorial, I’m currently trying to understand the process of Andengine, can’t wait to see more
Thanks! Hope to see more of these tuts. Your explanation of the methods and parameters is very clear. Keep up the good work.
Hey these are nice tutorials. I am new to andengine and would really like it if u could make some more beginners tutorials. Thanks a lot!
Nice tutorial! I’m new to andengine and have been looking all over for some tutorials that go from zero to simple but complete game. Thanks for writing this one, and please keep writing more! You’re a real help to noobs like me
Thank you for explaining everything clearly and simply for people new to programming.
I’m getting a force close error whenever I run this, I’m suspecting it has something to do with my installation of AndEngine. Can anyone help please
Hi, I got error when try to running, it says:
[2012-07-01 07:18:08 – Android] Could not find Android.apk!, I looking into /bin, I found jar files instead of apk??
Go to Project->Properties
Select Android from left-hand side list
Uncheck the “Is Library” checkbox
or
Deleted R.java then relaunched Eclipse
http://stackoverflow.com/questions/4778113/android-eclipse-could-not-find-apk
Hi, thank you that’s work…:)
Here’s the solution :
– right click on your project under Package Explorer tab, choose Build Path > Configure Build Path.
At the left pane, choose Java Build Path.
At the right pane, under Order and Export tab, choose Select All button and click OK button at the bottom of Properties window.
Unfortunately, “App name” has stopped. How do you solve that?
Hey.. I am getting into AndEngine coding. I got the same problem too. Did you fix it? Please tell me…:)
I’m getting the error message “The JAR file [path to…]\android.jar has no source attachment.” Do you know what I can do to fix this?
Hey,
Try going to the project properties -> Android and select one of your installed android version.
You are so cool! I’ll keep on checking your website for more of your tuts.
You’re my savior!
Very nice tutorials, every parameters are explained really well. Good job!
Hi! I like very much your blogging style. I’ve just few questions… How many gameactivities ot is possible to use? What are the differences? What’s the difference between a camera and a scene? There’s an official doc for gles2? It’s a little bit frustrating to use “others” examples trying to understand how stuff works without clear documentation… Thanks!
Hi,
No sadly there is no official documentation for GLES2.
The CAMERA is like your eyes, it shows you a part of the scene. Without a camera you wouldn’t be able to see anything. It shows you what’s happening in the Scene.
The SCENE is where all the magic happens, like your player moving left and right.
Keep em comin please
Thanks for helpfull tutorial.
Hey cool tutorial and helpful thanks.
Thank you so very much – I’ve been going through tutorial after tutorial trying to figure out how the heck to use AndEngine. All of the others I’ve found are either outdated, poorly written, or just throw up strange errors with no explanation, but this tutorial not only works, it actually explains WHY it works/what things do.
Thanks!
…. has stopped unexpectedly
My LoagCat
03-01 07:20:55.208: D/AndEngine(578): AndEngineTutorialActivity.onCreate @(Thread: ‘main’)
03-01 07:20:55.289: D/dalvikvm(578): Trying to load lib /data/data/com.PerleDevelopment.AndEngine.tutorial/lib/libandengine.so 0x405146e8
03-01 07:20:55.298: D/dalvikvm(578): Added shared lib /data/data/com.PerleDevelopment.AndEngine.tutorial/lib/libandengine.so 0x405146e8
03-01 07:20:55.298: D/dalvikvm(578): No JNI_OnLoad found in /data/data/com.PerleDevelopment.AndEngine.tutorial/lib/libandengine.so 0x405146e8, skipping init
03-01 07:20:55.398: D/AndEngine(578): AndEngineTutorialActivity.onResume @(Thread: ‘main’)
03-01 07:20:55.638: D/libEGL(578): egl.cfg not found, using default config
03-01 07:20:55.638: D/libEGL(578): loaded /system/lib/egl/libGLES_android.so
03-01 07:20:55.718: W/dalvikvm(578): threadid=10: thread exiting with uncaught exception (group=0x40015560)
03-01 07:20:55.739: E/AndroidRuntime(578): FATAL EXCEPTION: GLThread 11
03-01 07:20:55.739: E/AndroidRuntime(578): java.lang.IllegalArgumentException: No EGLConfig found!
03-01 07:20:55.739: E/AndroidRuntime(578): at org.andengine.opengl.view.ConfigChooser.chooseConfig(ConfigChooser.java:183)
03-01 07:20:55.739: E/AndroidRuntime(578): at org.andengine.opengl.view.ConfigChooser.chooseConfig(ConfigChooser.java:157)
03-01 07:20:55.739: E/AndroidRuntime(578): at android.opengl.GLSurfaceView$EglHelper.start(GLSurfaceView.java:919)
03-01 07:20:55.739: E/AndroidRuntime(578): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1264)
03-01 07:20:55.739: E/AndroidRuntime(578): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)
03-01 07:20:55.928: D/AndEngine(578): AndEngineTutorialActivity.onPause @(Thread: ‘main’)
03-01 07:20:58.507: D/AndEngine(578): AndEngineTutorialActivity.onDestroy @(Thread: ‘main’)
03-01 07:20:58.617: D/AndEngine(578): UpdateThread interrupted. Don’t worry – this EngineDestroyedException is most likely expected!
03-01 07:20:58.617: D/AndEngine(578): org.andengine.engine.Engine$EngineDestroyedException
03-01 07:20:58.617: D/AndEngine(578): at org.andengine.engine.Engine.throwOnDestroyed(Engine.java:574)
03-01 07:20:58.617: D/AndEngine(578): at org.andengine.engine.Engine.onTickUpdate(Engine.java:560)
03-01 07:20:58.617: D/AndEngine(578): at org.andengine.engine.Engine$UpdateThread.run(Engine.java:820)
03-01 07:20:58.707: D/AndEngine(578): AndEngineTutorialActivity.onDestroyResources @(Thread: ‘main’)
03-01 07:20:58.707: D/AndEngine(578): AndEngineTutorialActivity.onGameDestroyed @(Thread: ‘main’)
And my console
[2013-03-01 07:20:34 – AndEngineTutorial] ——————————
[2013-03-01 07:20:34 – AndEngineTutorial] Android Launch!
[2013-03-01 07:20:34 – AndEngineTutorial] adb is running normally.
[2013-03-01 07:20:34 – AndEngineTutorial] Performing com.PerleDevelopment.AndEngine.tutorial.AndEngineTutorialActivity activity launch
[2013-03-01 07:20:35 – AndEngineTutorial] Automatic Target Mode: using existing emulator ’emulator-5554′ running compatible AVD ‘andEngine1′
[2013-03-01 07:20:35 – AndEngineTutorial] Uploading AndEngineTutorial.apk onto device ’emulator-5554′
[2013-03-01 07:20:39 – AndEngineTutorial] Installing AndEngineTutorial.apk…
[2013-03-01 07:20:53 – AndEngineTutorial] Success!
[2013-03-01 07:20:53 – AndEngineTutorial] Starting activity com.PerleDevelopment.AndEngine.tutorial.AndEngineTutorialActivity on device emulator-5554
[2013-03-01 07:20:54 – AndEngineTutorial] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.PerleDevelopment.AndEngine.tutorial/.AndEngineTutorialActivity }
nice
Leave a comment
Thanks for the tutorials, they helped me get started. Awesome!
[…] /bin in my project folder, it contains jar file but not apk??? I’m following this tutorial : http://perle-development.com/tutorials/andengine-tutorial-01-creating-a-scene/ for that project. (Sorry for bad English […]