5

I'm an dev still learning in Android, I've created two apps so far, an alarm clock, a widget and a pass manager using databases, I have a little bit of experience, but I'd like to create a 2D side scroller game, I check on the web and there are different tutorials, but, what's the best way to start working on it? I've read about libgdx but I'm not sure if it's outdated.

I've seen that all the games are made in Java, and then ported to Android, is this correct? I would appreciate some guidance, thanks!

saman0suke
  • 762
  • 1
  • 8
  • 24
  • 5
    LibGDX is absolutely not outdated, in fact, v1.1.0 came out about a month ago and brought in iOS support to its multiplatform repertoire. This is my favorite tutorial http://www.gamefromscratch.com/page/LibGDX-Tutorial-series.aspx but this one is specifically about 2d platformer http://obviam.net/index.php/getting-started-in-android-game-development-with-libgdx-create-a-working-prototype-in-a-day-tutorial-part-1/ – EpicPandaForce Jul 14 '14 at 15:03
  • Thanks for the suggestions, I'll check on the tutorial and make a few tests when I have time. – saman0suke Jul 14 '14 at 15:20

2 Answers2

13

You have multiple options, you can either go for AndEngine (which to me seemed extremely underdocumented and random), make your own "native" Android game with extending from a SurfaceView (which isn't impossible but it certainly doesn't make your life easy, especially when handling images and especially sound, but here's a setup for it: Using a custom SurfaceView and thread for Android game programming (example)), and there's LibGDX.

I personally recommend LibGDX, I even made a fairly simple 4-player multiplayer game in it and it certainly was not difficult. I'd recommend the following tutorial on how to get to it: http://www.gamefromscratch.com/page/LibGDX-Tutorial-series.aspx

And the basics are the following:

  • When you create a project, the first thing you want to do is change the ApplicationAdapter to Game so you'll have access to the setScreen(Screen) delegation function, so that you can seperate the display and logic of your game into Screens.

  • You want to handle elapsed time in your Screen, which is done as the following: How to track time in Libgdx(android)

  • You probably want to make a menu, which of course can be done with pretty pictures and BitmapFonts, but I'll point you to the official wiki ( https://github.com/libgdx/libgdx/wiki ) with that. You can use Scene2D, although I found it slightly difficult, so I personally made a menu made of rectangles, it worked fairly well: LibGDX - Custom Click Listener?

  • A bit more "click oriented" guide on how I handled touch events using LibGDX: https://stackoverflow.com/a/24511980/2413303

  • Afterwards, it's literally just implementing game logic, timers, data models, behavior.

The way I solved the stretching rather than using a StretchingViewport or the in-built cameras was the following:

public class Resources
{
    public static Texture texture;
    public static SpriteBatch batch;

    public static Matrix4 normalProjection;
    public static BitmapFont bitmapFont;

    public static ShapeRenderer shapeRenderer;
    ....
}

public static void initialize()
{
    int width = Gdx.graphics.getWidth();
    int height = Gdx.graphics.getHeight();
    Resources.bitmapFont = new BitmapFont();
    Resources.shapeRenderer = new ShapeRenderer();
    Gdx.gl.glLineWidth((width < 640 && height < 480) ? 2.5f : 6f);
    //camera = new OrthographicCamera(1, h / w); //I didn't use this at all
    Gdx.gl.glViewport(0,  0,  Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    loadTextures();

    Resources.batch = new SpriteBatch();
    Resources.normalProjection = new Matrix4().setToOrtho2D(0, 0, 480, 320); //model is 480x320
    Resources.batch.setProjectionMatrix(Resources.normalProjection);
    Resources.shapeRenderer.setProjectionMatrix(Resources.normalProjection);
}


public class InputTransform
{
    private static int appWidth = 480;
    private static int appHeight = 320;

    public static float getCursorToModelX(int screenX, int cursorX) 
    {
        return (((float)cursorX) * appWidth) / ((float)screenX); 
    }

    public static float getCursorToModelY(int screenY, int cursorY) 
    {
        return ((float)(screenY - cursorY)) * appHeight / ((float)screenY) ; 
    }
}

Make sure to dispose resources that need disposing, in the Game's dispose() callback.

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • 4
    I agree, libgdx would be the best option. It is easy to use, multiplattform and can speed up the development. – Robert P Jul 14 '14 at 15:30
  • Ok, so it seems like the best way is libgdx over AndEngine right? I'll check it out and see how it works, thanks – saman0suke Jul 14 '14 at 17:46
  • @saman0suke I won't say, that libgdx is the better way. Here (https://groups.google.com/forum/#!topic/android-developers/xR6e9JNpwWM) you can read a little discussion. The creators of both libraries are also there (Nicolas and Mario) and talk about the engines. – Robert P Jul 15 '14 at 06:06
  • Hah, even if they'll tinker it up, there's nothing that beats an official wiki and actual documentation to the engine. LibGDX was really intuitive, even if Scene2D wasn't as much. – EpicPandaForce Jul 15 '14 at 06:16
  • @Zhuinden i personally really like Scene2D, especially for the UI. But it can also be used for the game it self. As i said i never used AndEngine, but there are many people out there who like it, so i think it has some advantages too. – Robert P Jul 15 '14 at 06:59
  • @Springrbua I'm pretty sure the inner workings of Scene2D are much better than just using mere lists, so there is a certain level of complexity over which it's better to use the cameras and scene graphs provided by LibGDX. But even if you don't, it already helps you greatly with just the application lifecycle, file handling, textures and sound. – EpicPandaForce Jul 15 '14 at 07:54
  • @Zhuinden how do you mean that? As i said i am also using libgdx, i really like its lifecycle, scene2d helps a lot by organizing the UI and there are many helpfull classes included. But as much as i understood AndEngine (again i never tryed it) is easier for beginners, but i have to say i also learned libgdx pretty fast. – Robert P Jul 15 '14 at 08:26
  • @Springrbua I can't imagine how it can be any easier than LibGDX. o_o i just found scene2d and using "actors" and stuff kinda confusing, I wanted to keep the object coordinates in my own hands – EpicPandaForce Jul 15 '14 at 08:32
  • 1
    @Zhuinden well everything is confusing at the beginning, but as soon as you understand it scene2d can help a lot. The only big disadvantage is, that the logic and the view (`update()` and `draw()`) are in the same class, which makes MVC almost impossible. – Robert P Jul 15 '14 at 09:27
  • I'm trying to follow the tutorial posted here, but the jar that I found in the libgdx site creates different objects (might be newer, is there any updated tutorial to start with? thanks! – saman0suke Jul 22 '14 at 19:42
  • Are you using Scene2D or your own stuff? – EpicPandaForce Jul 22 '14 at 23:02
  • Right now, I have used only the setup tool in the libgdx's website, and that generated a project for desktop, core, android, none of them runs. – saman0suke Jul 23 '14 at 14:15
  • Okay well that's interesting, do you have the GRADLE tool installed in Eclipse? ( http://stackoverflow.com/questions/24031573/opening-libgdx-project-in-eclipse-adt-bundle ) – EpicPandaForce Jul 23 '14 at 14:21
  • Yes, I got it, that's what I used to import the project generated, but when I try to debug the desktop project (for example) there is no main class...I feel like I have something different than all the tutorials show. – saman0suke Jul 23 '14 at 20:37
  • you should be able to run the project on the ApplicationLaunchers in yourgame-desktop and yourgame-android – EpicPandaForce Jul 23 '14 at 21:23
  • I ran it using DesktopLauncher, thanks for the tip! Android failed however. – saman0suke Jul 25 '14 at 14:35
  • Are you using the ADT? or Android Studio? – EpicPandaForce Jul 25 '14 at 14:36
  • ADT. I just tested it in the real device and it worked, maybe it's because when I run it, it opens an emulator with android 2.3, I think I need a newest version of Android, but I don't see where I can select a different emulator, thanks! – saman0suke Jul 25 '14 at 15:02
  • Oh I'm guessing the problem is that the default emulator doesn't support OpenGL ES 2.0 and LibGDX uses it. Otherwise, any device above the level of 2.3 and above is able to run LibGDX-made applications. – EpicPandaForce Jul 25 '14 at 15:08
  • I'd like to focus in the Android version of the game, do I create all the logic there, or should I use the one called "core"? thanks! – saman0suke Jul 28 '14 at 19:18
  • The "Android" version is just a launcher for the Android version. Basically it runs an Activity (although I was debating using `initializeForView` and load it into a Fragment for extra stability) with the core's Game implementation in it, and thus the Core provides all the logic. The first thing you want to do is change the `ApplicationAdapter` to `Game`, then start crackin' with `Screen`s. – EpicPandaForce Jul 28 '14 at 20:01
  • 1
    @saman0suke thats the big advantage of libgdx. You code 1 game ("Core") and with a few lines of code ("DesktopLauncher", "AndroidLauncher") you can port it to android, Desktop, HTML5 and even iOS. Ofc there are a few things you need to take care about, when you are targeting a mobile device (for example the input). So all your logic should be inside the "core" project. – Robert P Jul 29 '14 at 07:04
7

Libgdx is not outdated and is, IMHO, the best way to program for android. The reason ist, that you can develop 99% on desktop (ofc think about the controlls, which won't be a keyboard on android) and then you have a working android app with a few lines only.
If you instead develop for android directly, you need to use the verry slow emulator or you have to send the app to a testphone, just to debug your code. This is a lot slower then debuging on desktop directly.
Libgdx is verry efficient, easy to use (as soon as you understan how it works) and has a verry good documentation.

For tutorials: I wrote an answer here on SO, which seemed to help some people. It is a short "tutorial" which shows only the verry basics and i have added the links to some tutorials which helped me learning it. So i hope it helps you to^^

Community
  • 1
  • 1
Robert P
  • 9,398
  • 10
  • 58
  • 100
  • 1
    GenyMotion is a much better emulator than the default one, but it's still more convenient to test on a desktop. – EpicPandaForce Jul 14 '14 at 15:19
  • @Zhuinden i never worked with an emulator, as my (never finished :P) games were always targeting the desktop. But with a few code changes (the controlls) and a few additional lines (the android backend stuff) they would work on android (if they were working for desktop :P). – Robert P Jul 14 '14 at 15:32
  • Isn't bluestacks a better emulator than Genymotion? – Boldijar Paul Jul 14 '14 at 18:11
  • @Paul does Bluestacks have a debug-mode or can you only test the finished app? – Robert P Jul 15 '14 at 06:00
  • @Springrbua I don't know, but when I used it worked perfectly with my games, high FPS..after i bought my phone i never used an emulator anymore.. – Boldijar Paul Jul 15 '14 at 09:20
  • @Paul the thing is, that you always have to load the app to the phone, while with libgdx you can develop it on desktop and test it on desktop. And with only a few lines of code you have an adroid app. – Robert P Jul 15 '14 at 09:28
  • @Springrbua yea, but if you want to test something like ads or android stuff from libgdx and don't have a phone, you need an emulator. the default android sdk emulator doesn't even work with libgdx as far as i know – Boldijar Paul Jul 15 '14 at 12:21
  • @Paul ah okay, did not know that. Well the final testing is always on the phone, or better on many phones. Just cause of the different screen sizes and the lower performance, as well as real touch and multitouch. – Robert P Jul 15 '14 at 12:23