139 comments on “AndEngine Simple Android Game Tutorial

  1. Thanks a lot it is really helpful ! i began today to use andengine and your tutorial is just legen -wait for it – dary ! :)

      • Hey ! Just a fix I had to do.

        When we add the font texture for the score, you forgot to show the mFontTexture creatiobn. I had an error because of that , in the loadRessources() :

        add this :

        mFontTexture = new Texture(512, 512,
        TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        ( i put the same number of the other but i m not sure!)
        if not, there is a bug because of that because mFontTexture doesn’t contain anything :

        mFont = new Font(mFontTexture, Typeface.create(Typeface.DEFAULT,
        Typeface.BOLD), 40, true, Color.BLACK);
        mEngine.getTextureManager().loadTexture(mFontTexture);
        mEngine.getFontManager().loadFont(mFont);

      • :) I have an other problem and I don’t know where it comes this time :/
        I created the onResume ( onREsumeGame seems to doesn’t exist anymore) and then the onPause and then game crashe directly at the beginning ><
        Without onPause it works… I am quite lost here

      • you are probably using an older version of the libraries ? [or maybe they changed it in the new one]
        try to download the library [the .jar file] that I provided in the tutorial and use it

  2. Hi. I’m from Brasil and i really liked your tutorial, it’s help me a lot. Keep up the goooooo0d Work, dude !

  3. Jimmar, Brother. I’m starting now with Android and i really like if you indicate me some kinds of tutorials of games in Android. Tks again !

  4. This is very nice. I started with AndEngine today, and I think that you’ve created very nice intro to get started. Thanks!

  5. Thank you very much from a French :-) , I’m beginner so I’m not sure! But I think there are little mistakes like :

    two -> return super.onKeyDown(pKeyCode, pEvent); in onKeyDown

    and we need to add :

    mMainScene.attachChild(score);

    before
    return mMainScene;
    in onLoadScene()

    but it’s easy to find thanks to your great explanations on andengine

    • yeah , because of the structure of the way I handled it .. I kinda had to have two return statements .. feel free to do it in another way
      and for the score, you are already doing that inside the method “restart()” ;)

  6. oh and change :

    if (!backgroundMusic.isPlaying()) {
    resumeMusic();
    mEngine.start();
    }

    to

    if (!backgroundMusic.isPlaying()) {
    resumeMusic();
    unPauseGame();
    mEngine.start();
    }

    in onKeyDown

    • already mentioned that later on in the tutorial “And onKeyDown gets changed a li’l bit again, set the pauseFlag to true after you call pauseGame(), and to false after you unPauseGame() it”
      well .. no harm into editing it again

  7. Hi,

    trying to show your png files on the screen an error occurs: “the application has stopped unexpectedly.”

    Are your images right? it works if I try woth other png files.

    thanks!

  8. hello I’ve been trying to work off your example here, and have not been able to get the sprite to display at all. scene.attachchild() seems to be giving me the trouble, eclipse says it throws and illegal state exception and Logcat gives me an error claiming “Error while generating scanline”
    I would love to get this running, but I’m at a road block if I can’t get sprites onscreen.

    • umm.. I can’t really figure out what’s exactly wrong with only this info
      paste what you have so far here pastebin.com
      and post the link , maybe you missed a li’l part or something

  9. Just freakin’ awesome!! Thanks a ton for this! Before this one I was trying to create my own game engine and do all the stuff myself. While that was good for learning, it was taking too much time and I wasn’t able to concentrate on the business part of game development (ahem!). AndEngine is great and greater is this tutorial for starters!

    Thanks and a beer when we meet! :)

  10. Hi!
    Been trying to understand andengine for a couple of days now and your tutorial is the most useful and easiest I have come across.
    I just began following it, but found a problem at the very beginning, when it is supposed to show the blue screen, all the emulator outputs is a landscape screen showing “Hello World, GameTestActivity!” (Being GameTestActivity the name of my class). What am I doing wrong?

    Thanks!

    • when you create the class [GameTestActivity] make it extend BaseGameActivity in the create window [or just delete the oncreate() method], your class should not have onCreate() method in it , I’ve updated the post with a screenshot

  11. What is this statement if: if (_target.getX() removeSprite(_target, targets);

    in:

    01 IUpdateHandler detect = new IUpdateHandler() {
    02 @Override
    03 public void reset() {
    04 }
    05
    06 @Override
    07 public void onUpdate(float pSecondsElapsed) {
    08
    09 Iterator targets = targetLL.iterator();
    10 Sprite _target;
    11
    12 while (targets.hasNext()) {
    13 _target = targets.next();
    14 if (_target.getX() removeSprite(_target, targets);
    15 }
    16 }
    17 targetLL.addAll(TargetsToBeAdded);
    18 TargetsToBeAdded.clear();
    19 }
    20 };

    I got the error

  12. Extremely beautiful. The code is simple, understandable, and covers a lot of the basics. I cannot thank you enough for your work.

    Just one note, the project worked perfectly on the emulator. Later wanted to test it on my galaxy i9000, and the app crashed on the first attempt to launch. Worked perfectly on the following attempts though.

    Thank you for the amazing work again!

    • Glad you enjoyed it !
      I own the same phone [high 5 ?] and sometimes eclipse doesn’t get updated with all the assets when deploying the app , or it could be a random problem … inform me if it happens again [with the log (stacktrace) from logcat]
      and you are welcome :D

  13. Very Nice tutorial I have never found tutorial good like this , Thank you.

    But I didn’t understand linkedlist part can you explain what is linkedlist ?

    • even though this is out of the scope of the tutorial [and I gave you a wikipedia page] but here’s a brief
      the idea of the linked list is to have a node that points to another node, each node would be a class (or struct) that contains the data and a pointer to the next node i.e.
      class node{
      string data;
      node* next;
      }
      whenever you need to add a new element, you’d create a new node and make the last node point to this new one , you end up with a list of nodes that are linked to eachothers , and to access a node in the middle you’d have to go from the head and iterate over the pointers till you reach the one you need.
      here I used a ready made linkedlist provided by java, you can create your own if you need it
      you may need to look up more data structures tutorials to get the hang of it

  14. First thanks for this great and clear tutorial. All is fine untill i got stuck on the part ‘But .. But .. They are not dying ! (collision detection)’. When i update my detect IUpdateHandler to yours i get all sorts of misconstruct errors. Am i doing something wrong or is the code damaged?

    • urgh , wordpress omitted a small part from code >_<
      I changed it back and should be fixed now .. if you have any other issues please post the stack trace you get from logcat [DDMS] (assuming you are not getting compile errors) and your code on http://pastebin.com/ and post the link here
      I'll check it for you

      • HI, i think there is always a mistake in this part with collisions…. I have a lot of mistakes when i’m adding this code.

        thx

      • I’m pretty sure that I fixed everything :S .. could you give more info about what errors are you getting from this ?
        also .. go to the bottom of the tutorial and download the whole project then check if you can figure out what’s the difference .. I can’t exactly pin point what’s wrong in there
        if you need help getting the difference , check my previous comment on how to let me know

  15. This is wrong in the blog: (IUpdateHandler::public void onUpdate)

    if (_projectile.getX() >= mCamera.getWidth()
    || _projectile.getY() >= mCamera.getHeight()
    + _projectile.getHeight()
    || _projectile.getY() removeSprite(_projectile, projectiles);
    continue;
    }

    should be (from downloaded source):

    if (_projectile.getX() >= mCamera.getWidth()
    || _projectile.getY() >= mCamera.getHeight() + _projectile.getHeight()
    || _projectile.getY() <= -_projectile.getHeight()) {
    removeSprite(_projectile, projectiles);
    continue;
    }

    and big thanks for the great tutorial :)

    • Thank you for reporting that !
      I got no idea what’s happening here .. guess wordpress made some changes or something is getting parsed as HTML tags
      tell me if you found any others
      and glad you liked it :D

      • only a small thing:

        backgroundMusic = MusicFactory.createMusicFromAsset(mEngine
        .getMusicManager(), this, “background_music.wav”);

        the file from Dan (the one you linked) is named “background_music_aac.wav”

        small but leads to a exception/force close :)

  16. Hi again, I want you ask simple basic java question :)

    On this function . removeSprite(_target, targets); How Can it removing sprite I didn’t get it…

    public void removeSprite(final Sprite _sprite, Iterator it) {

    runOnUpdateThread(new Runnable() {

    @Override

    public void run() {
    mMainScene.detachChild(_sprite);

    }

    });

    it.remove();

    }

    How is this removing targets from program.
    while (targets.hasNext()) {
    14
    _target = targets.next();
    15

    16
    if (_target.getX() <= -_target.getWidth()) {
    17
    removeSprite(_target, targets);
    18
    break;
    19
    }

    can you explain a bit ? because on c++ we can't delete other variables which is not in functions. without pointers but here we are deleting…

    • from the Java docs , the descriptor for the iterator remove() method is
      “Removes from the underlying collection the last element returned by the iterator (optional operation).”

      the removeSprite() method takes 2 arguments , the _target object which is the target we wanna remove, and the iterator.
      the iterator goes over the linkedlist objects (it just goes over them , doesn’t copy the contents) so when you remove using the iterator remove() method you are actually also removing from the linked list, and it removes the last object it reached

      if you removed directly from the linked list , it’ll throw an exception cause the size of the linked list changed , but if you are using the iterator it’ll be handled properly

      [almost] everything in Java is an object, so you may consider the iterator as a pointer going over the linked list

  17. Thanks Again I understood object part. Have I understood Correct( iterator part )?

    we are using targets.next() it is skipping to next item in the list and when we use targets.remove() it is removing item where it stopped.

  18. This is great! Though I have to ask…how would you add a background image? If you added another image, wouldn’t that cover the animation?

  19. ;) Thank you!! my will be uploaded to android in few days using this Engine and will be next big thing i hope!! :D very good tut never got lost :D like in most tut in books or websites :P, thank you!!!!!!!!!!!!!

  20. I am pretty sure I am doing something wrong but I have no idea what. I have gone through the code several times. My problem is in the onLoadResources(). Where you initialize the texture regions.

    It appears to draw the images on the screen right then and there. For example, when i play the game, I have the “fail” image, “win image”, “ninjas”, “shuriken”, “enemies” and every single image i initialize already drawn.

    My fix for it was to change the coordinates when I initalize them too off the screen. i used “mCamera.getWidth()” for the x value instead of the one you used and i changed up the Y value too. So now my game works like yours but I would still like to know the cause of the problem.

    Any ideas on what I did wrong?

  21. Jimmar, i have added different enemies. But i want to remove them only when the user touch in enemy in the screen. The problem is: how i do this ? Thank you for help !

  22. Hi ! i am new in AndEngine… and your tutorial helped me a lot! However after i did everything you did and its done good, now i am messing with how to do the image background (another image that i add)
    I know i need to do sprite first off all, but when i add the sprite to between mMainScene = new Scene(); and return mMainScene; he just give me ann error (Force close) but for example if i add the sprite to mPauseScene or mResultScene its show up without problem, cant understand… :(
    So waiting for answer, thank you soo much :)

    • I’m not exactly sure how you are putting the background image , but if you are adding it as a big sprite then you can’t add it inside onLoadScene cause you are adding the image to the scene when the scene is not ready
      if you wanna add it as a normal sprite then attach it inside onLoadResources().
      if it’s still not fixed I’d appreciate if you paste your code on http://pastebin.com/ and gave me the link
      also if you checked the DMMS/Logcat , it should give you a stack trace of the error and why it happened [the exception name] provide that too as just saying “ForceClose” is not helpful
      you might wanna continue with the tutorials series too, it includes some neat background and animations later on :P

  23. Tnx for answer! Well from now i have the source code that you give with the tutorial (I wrote everything step by step and everything was good but after some mess with the code he gave me alot of errors so i downloaded the source code )
    and of course i add it in onLoadResources(). to…
    Can you tell me how can i add big sprite (Or is it the same as normal Sprite function ?) again thank you !

    • adding a big sprite is the same, just make sure you understand how BitmapTextureAtlas works and that you have enough space to add that
      and again, if you continued with the tutorials series [there are 2 more tutorials that continue on this one, the next one got a link in the last section of this one] you would see how to add a big sprite

    • am sorry but I didn’t exactly understand what you mean , I explained everything clearly in the tutorial but if there is an issue please provide more info

  24. Nice tutorial. Just looking for some help with an issue I’m having with installing the extensions for the game engine. There are no errors in the code for the game engine itself, but as far as the extensions are concerned, they’re registering a ton of source code errors. Any idea on how to fix this? Thanks in advance.

    See ya on the flipside,

    Tempest Desh

    • are you talking about extensions like multiTouch and box2d ?
      in tutorial V2.0 I show you how to install multiTouch extension [more or less :P] but it appeared that the multitouch source code actually includes the andEngine source code too so you’d have to get rid of that .. at least that was how the case was with me

      if you are talkin about something else, then would you give me more info about what are you talkin about ?

  25. Hello Jimmar,
    How are you? The tutorial is awesome and helped very much.
    i want to take ur opinion about something, Is it better to make one IUpdateHandler to detect all events or to make 2 or more handlers (one for sprites, one for bullets,,..etc)?

    Waiting for ur reply
    Regards

    • glad you liked the tutorial
      I’m not quite sure , but I think that it’d be better to have one update handler [main game loop] unless it doesn’t work well [if it lags or some actions needs priority] then use multiple

  26. Hello Jimmar,
    Thanks for your reply,
    Please i need your help with something strange. i need the .jar extension files of multiTouch and other extensions but i failed to get them. i tried to clone the extensions from google.code but i failed :(
    if u have them or know how to get them, please tell me.

    Thanks again,
    Hossam

  27. Hello Jimmar,
    i have put a pause in my game as u teaches us in ur tutorial, but i have a problem. i put sprites in the pause scene and touch listener on them, (ex: restart images when the player pause the game i put a sprite with restart image) but it doesn’t respond!!!
    i have enabled the touch in the scene :
    pauseScene.setTouchAreaBindingEnabled(true);
    and register to the sprite :
    pauseScene.registerTouchArea(go_btn);
    the image of restart appeared but no touch. Could u tell me why this issue? i guess it is because the mEngine.stop() ?
    waiting for ur reply

  28. Very good tutorial and helped my understand the basic concepts of the andengine examples and how to build the basic game, I actually was able to add some personal touch such as sound upon hitting the target ad sound when loosing / wining the game. This was very helpful indeed.

  29. Hi, I’m beginning with AndEngine and was having an hard time finding a good game tutorial. Yours is really great, it helped me a lot understanding how it works and how to do in futurs projects.

    Thank you very much and keep going :)

  30. Hey Jimmar,

    Great! Great tutorial! I’m loving it. As a starter on game development, this is big to me. I just found out about AndEngine few days ago and dying to find good tutorials. Yours is great!

    Quick basic question though as I am getting myself familiar with the engine. Is mEngine one if the class in BaseGameActivity? I have seen few tutorials using mEngine without any explanation.

    • I have answered this in a previous comment .. but oh well
      you can put a sprite with the same size as the screen on the back ..
      you can also continue with the tutorials to see how to make a moving background

  31. Hey, seems to be a very great tutorial!

    But why do i get error messages all the time about the code in eclipse, i follow tutorials but all these errors are coming up. Why is that? Maybe if you know… thanks

    • I can’t figure out what’s wrong with those info only , could you at least tell me what kind of errors are you getting ?
      keep in mind that this is done on AndEngine GLES1.0 not GLES2.0 , if you are using GLES2.0 this won’t work without modifications
      try using the jar file that I provided and see if that solves the problems

  32. I did use your jar file in /lib, i’m not that good at coding but when i follow every tutorial i do as people write it and still get all these errors on every line? Maybe i have installed eclipse and so on wrong, if i write some lines of codes that is being used here. It will give X dots everywhere and tell me to maybe create a class or they have another quick fix for it. Every god damn line, if you know anything about it i would be very greatful! Thanks.

    • Try cleaning the project [project -> clean]
      also , are you sure you installed andEngine properly ? have you managed to run your code till the blue screen ? [if this didn’t work then you might not have added the jar file to your build path, go to it in eclipse and right click on it -> build path -> add to build path]
      and before that , did you run an android hello world program to make sure that the android sdk is installed properly ? [you may wanna follow an android beginner tutorial first to make sure it’s working]
      and don’t copy and paste code , write it on your own :D

  33. der zabardast tutor dy yar manam dy(in pashto) means
    this is awsome tutorial for beginners

  34. Very good tutorial for a beginner and thank you for making it it!
    I’m trying to make the part with the blue screen to test if the engine works but some code lines have errors (the x dot). I added those two permisons to the manifest and installed andEngine.jar ( right click on it -> build path -> add to build path ). Also I made a few Hello World android apps in eclipse and it worked so I don’t have a problem with eclipse. If you know what is wrong is please tell me!

    • Try restarting eclipse, but are you sure that you put andEngine.jar file in the correct place ?
      try this too

      Right click on the project visit properties , go to java build path and then Order and Export and make sure everything is ticked

  35. great tutorial as i read it and basing from all the comments! but the thing, i haven’t tried it yet since i am really newbie in using andengine. Just need a little help Jimmar, can you help me how to install andengine /GLES1.0? i already have my androidSDK and eclpise up and running. also done with hello world tutorial, and it works just fine. your reply would be much appreciated! thank you again and God bless!

    • you can use the tutorial I linked to that installs GLES2.0, just chose “master” instead of GLES2.0
      or you could just use the andengine.jar file that I provided in this tutorial
      you could also skip this whole tutorial and jump to my jimvaders tutorial on AndEngine GLES2

  36. hello, I am following your tutorial and place of testing to see if the blue screen appears, the game does not open. The engine is in the Build-path correctly.

  37. how to add background with scroll, i work in strategic game,
    help how to change splash to activity menu no workme,

    ps.good good good tutorial.

    • if you want to automatically scroll the background use parallax Background.
      and that’s out of the scope of this tutorial anyway , I suggest you ask your question at the AndEngine forums

  38. Hi JiMMaR,
    First, sorry for my poor English =S
    Second, thks for this incredible tutorial!!! =D
    And to finish… I try the game in the Eclipses Android Emulator (android 2.2 version) and it runs right but when i try it in a real mobile phone (with android 2.3) it crashes when i try to execute.
    Thks for all and continue with this tutorials, they are rly ussefull!!!!

    • Those Info are not enough , you need to check what the catLog says regarding the crash [the cause of the crash]
      if you couldn’t figure it out , use http://pastebin.com/ to paste the output and put the link here

      DO NOT POST THE WHOLE LOG AS A COMMENT OR I’LL DELETE IT !!

  39. Sir i have no word to saying Thank you for sharing this tutorial.
    please send me important links for study of android engine.

  40. I was excited to find this great site. I need to to thank you for your time for this fantastic read!! I definitely really liked every bit of it and i also have you book-marked to look at new information on your blog.

  41. Hi Jimmar.
    Thanks for this great tutorial! It has definitely helped me understand the things that I was struggling with. Nice site too dude :)

  42. Hi Jim,

    imported your project into my workspace, it was complied well, but I had a run-time error (COPY FROM LOGCAT):

    FATAL Exception : GLThread 11
    java.lang.IllegalArgumentException : No EGLConfig found !
    …. …..

    Any ideas what is missing?

    • err .. This tutorial was meant to be followed , and not imported as a whole
      also , this is for GLES1.0 , you should go to the other tutorial called jimvaders for GLES2.0 tutorial
      I don’t really know exactly what’s wrong with your project

  43. hey so i have been looking true the tutorial and like like it vary much but i can’t get it to work if im importing is as a git hub project in any why :( and the download link is down

    • as I told the other guy , this tutorial is mainly meant to be followed not imported
      also .. I think you posted on the wrong thread .. I didn’t provide a github link for this tutorial

  44. Nice tutorial.

    I was asking my self how to implement different levels. I was thinking to use the CameraScene object and do something similar as you do to pause the game. Do you think this is the right way? Do you know any tutorial for creating different levels in the game?

    Thamks

    • I have 3 solutions that are coming to my mind
      1- have each level implemented as a different scene, you get to have full control over what happens in each level [add a camerascene for a loading page to appear at the end of each level and a similar one to be attached at the start of the level to mimic a transition]
      2- if your levels only have different enemies , just change the enemy layer, attach different enemies to a new enemy layer and replace the one you already have with the new one [easiest solution :P]
      3- if the game grows big , it’d be easier for the long run to be able to design your levels by reading from external xml/txt file where you design the levels there

  45. Brilliant! Im a beginner to Android. I had to make an Android game as part of my school project. This has really helped me a lot. Thanks!

    Looking forward to much more from you. :)

  46. I can’t seem to recycle anything? Bullet, and targets both aren’t being recycled. I’ve added the Iupdatehandler, registered it on createscene, and targets are being added, and so are bullets.
    Is this because I’m not using GLES 1.0? I’m using 2.0, and I know you made a new tutorial, but I just find this a little better. Can someone help me out? I’ve created 3 projects files already in case if I had made any mistake, but nothing seems to be working. It still shows in Logcat that target, and bullets are not being recycled.

  47. okay.. weird enough, I just booted eclipse today, and everyone is working fine. sometimes.. i just feel like setting eclipse on fire. :> Thank you so much for the tutorial! :] We all really appreciate the time and effort you put into them. keep it up, mate.

  48. sir can you please help me, I want to make the player on the right side of the screen, which makes the bullets go right to left, please help me with the formula, thanks

  49. hi ,your tutorial is nice,very helpful to me.
    i have one doubt ,how to give 3 chances to user the objcet is crossing the screen width.

  50. hi, jimmar
    sorry in advance if my english bad .. :)
    I tried to make a RPG game in android and using a background image. I build it in android version 2.3.1 (gingerbread) but still error projectku … is there a tutorial for the game with a background image?? thanks for the advice ..

    • if you continue with this series of tutorials, you should see how to create a moving background image in the 3rd tutorial [in Simple Android Game Tutorial V2.0]
      but if you want a static image , just put a sprite that covers the whole thing
      don’t use the background functions, a simple sprite would do fine

  51. Aw, this was a very good post. Taking the time and actual effort to make
    a really good article… but what can I say… I procrastinate a whole lot and don’t manage to get nearly anything done.

  52. Thank you, I have just been searching for info about this subject for a long time and yours is the greatest I’ve found out so far. However, what about the bottom line? Are you positive concerning the supply? dkeebfbdeefk

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s