如何将 libgdx 添​​加到现有项目 [英] How to add libgdx to existing project

查看:9
本文介绍了如何将 libgdx 添​​加到现有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提供了一个用 android 制作的应用程序,它有一个导航抽屉,里面有一个游戏列表.我必须创造另一个游戏并把它放在那里.必须由我创建的游戏必须使用 libGDX,但原始应用程序没有使用此库.

I have provided an aplication made in android that has a navigation drawer and in it has a list of games. I have to create another game and to put it there. The game that has to be created by my must use libGDX but the original application didn't use this library.

可以这样做吗?如果是,我如何将 libgdx 添​​加到现有项目中.在 github 上,我只找到了如何使用 libGDx 开始一个新项目,而不是如何将其添加到现有代码中.谢谢.

Is it possible to do this ? If Yes, how can I add the libgdx to the exiting project. On github I found only how to start a new project with libGDx, an not how to add it to exising code. thanks.

推荐答案

您可以通过以下两个步骤来实现:

You can achieve this with these 2 steps:

在您的 android gradle 文件上:build.gradle(应用程序或 android 模块)

On your android gradle file: build.gradle (app or android module)

dependencies {
    ...

    // Add this line, it is recommended to use the latest LibGDX version
    api "com.badlogicgames.gdx:gdx-backend-android:1.9.10"
}

2.为视图初始化 LibGDX 或使用 LibGDX 托管活动

选项 1:为视图初始化
由于您有一个导航抽屉和更多原生于 android 的代码,因此此选项更适合您的需求.来自这个问题(How to add LibGDX as a sub view in android):

AndroidApplication 类(它扩展了活动)有一个名为 initializeForView(ApplicationListener, AndroidApplicationConfiguration) 的方法,该方法将返回一个可以添加到布局中的视图.

The AndroidApplication class (which extends activity) has a method named initializeForView(ApplicationListener, AndroidApplicationConfiguration) that will return a View you can add to your layout.

-- Matsemann

还有这里's 文档关于如何实现这一点(基于片段的 LibGDX).

Also here's documentation on how to implement this (Fragment based LibGDX).

选项 2:使用托管活动
这是 LibGDX 的默认/最常见用法,您需要按如下方式更改代码:

Option 2: Use a managed activity
This is the default/most common use of LibGDX, you will need to change your code as follows:

  • 您的 Activity 需要扩展 Android 应用程序:
public class MainActivity extends AndroidApplication {

  • 创建一个扩展 Game 或 ApplicationAdapter 的类:
  • import com.badlogic.gdx.Game;
    
    public class LibGDXGame extends Game {
    
        @Override
        public void create() {
            System.out.println("Success!");
        }
    }
    

    • 初始化 LibGDX 托管活动:
    • import android.os.Bundle;
      
      import com.badlogic.gdx.backends.android.AndroidApplication;
      import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
      
      public class MainActivity extends AndroidApplication {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
              initialize(new LibGDXGame(), config);
          }
      }
      

      这篇关于如何将 libgdx 添​​加到现有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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