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

查看:19
本文介绍了如何创建一个Android 2D 游戏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名仍在学习 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上,对吗?我希望得到一些指导,谢谢!

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(在我看来它的文档非常少而且很随意),制作你自己的原生"Android 游戏,并从SurfaceView(这并非不可能,但它肯定不会让您的生活变得轻松,尤其是在处理图像和声音时,但这里有一个设置:使用自定义 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/page/LibGDX-Tutorial-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 更改为 Game,这样您就可以访问 setScreen(Screen) 委托函数,这样你就可以将你的游戏的显示和逻辑分离到Screens中.

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

您想处理屏幕中经过的时间,具体操作如下:如何在 Libgdx(android) 中跟踪时间

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

你可能想制作一个菜单,当然可以用漂亮的图片和 BitmapFonts 来完成,但我会给你指向 official wiki ( https://github.com/libgdx/libgdx/wiki ).你可以使用Scene2D,虽然我觉得有点困难,所以我个人做了一个由矩形组成的菜单,效果很好: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 处理触摸事件的更多点击导向"指南:https://stackoverflow.com/a/24511980/2413303

A bit more "click oriented" guide on how I handled touch events using LibGDX: https://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天全站免登陆