如何创建一个Android 2D游戏? [英] How to create an Android 2D game?

查看:186
本文介绍了如何创建一个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.

我见过的所有的游戏都是用Java做,然后移植到Android,这是正确的?我想AP preciate一些指导,谢谢!

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!

推荐答案

您有多种选择,你可以去AndEngine(这对我来说似乎非常underdocumented和随机),使自己的原生的Andr​​oid游戏与延伸一个SurfaceView(这不是不可能的,但它肯定不处理图像,特别是声音时,让您的生活更轻松,尤其是,但在这里是为它设置:<一href="http://stackoverflow.com/questions/24890900/using-a-custom-surfaceview-and-thread-for-android-game-programming-example">Using一个自定义的SurfaceView和线程为Android游戏编程(例如)),并有LibGDX。

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.

我个人建议LibGDX,我还送了一个相当简单的4个玩家的多人游戏在里面,它肯定是不困难的。我建议你​​如何得到它下面的教程: HTTP:// WWW .gamefromscratch.com /页/ LibGDX-教程series.aspx

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

和基本有以下几种:

  • 当你创建一个项目,你要做的第一件事就是修改 ApplicationAdapter 游戏所以你有机会获得 setScreen(画面)代表团功能,让你可以单独游戏的显示和逻辑到屏幕。

  • 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.

您要处理的经过时间在你的屏幕,这是做如下:<一href="http://stackoverflow.com/questions/23995854/how-to-track-time-in-libgdxandroid/23996041#23996041">How在Libgdx跟踪时间(安卓)

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

您可能想使一个菜单,当然也可以用pretty的图片和BitmapFonts做的,但我会指示你的官方维基 https://github.com/libgdx/libgdx/wiki )这一点。您可以使用Scene2D,但我发现它稍难,所以我亲自做矩形的菜单,它的工作相当不错:<一href="http://stackoverflow.com/questions/24219170/libgdx-custom-click-listener/24219776#24219776">LibGDX - 自定义点击监听器

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?

更​​有点点击为本我如何处理使用LibGDX触摸事件指南:<一href="http://stackoverflow.com/a/24511980/2413303">http://stackoverflow.com/a/24511980/2413303

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

予解决的拉伸,而不是使用一个StretchingViewport或内置相机的方式是如下:

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) ; 
    }
}

确认处置需要配置资源,在游戏中的的Dispose()回调。

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

这篇关于如何创建一个Android 2D游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆