How to create an Android 2D game?(如何制作安卓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(在我看来,这似乎是文档不足和随机的),通过扩展从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)
委托函数,让您可以将游戏的显示和逻辑分离到Screen中.
When you create a project, the first thing you want to do is change the
ApplicationAdapter
toGame
so you'll have access to thesetScreen(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来完成,但我会给你指点官方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.
这篇关于如何制作安卓2D游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何制作安卓2D游戏?
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01