Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an Android 2D game?

Tags:

android

2d

libgdx

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!

like image 417
saman0suke Avatar asked Jul 14 '14 15:07

saman0suke


2 Answers

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.

like image 84
EpicPandaForce Avatar answered Sep 28 '22 04:09

EpicPandaForce


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^^

like image 38
Robert P Avatar answered Sep 28 '22 04:09

Robert P