How to add libgdx to existing project

后端 未结 2 946
太阳男子
太阳男子 2020-12-19 13:17

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 ha

相关标签:
2条回答
  • 2020-12-19 13:27

    Generally what you want is possible, I reckon your best bet would be to create a new libgdx project with their GUI and to then manually merge the files that are needed.

    0 讨论(0)
  • 2020-12-19 13:30

    You can achieve this with these 2 steps:

    1. Add LibGDX to your project

    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. Initialize LibGDX for a view or use a LibGDX managed activity

    Option 1: Initialize for a view
    Since you have a navigation drawer and more code native to android this option fits your needs better. From this question (How to add LibGDX as a sub view in android):

    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

    Also here's documentation on how to implement this (Fragment based 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:

    • Your activity needs to extend Android application:
    public class MainActivity extends AndroidApplication {
    
    • Create a class extending Game or ApplicationAdapter:
    import com.badlogic.gdx.Game;
    
    public class LibGDXGame extends Game {
    
        @Override
        public void create() {
            System.out.println("Success!");
        }
    }
    
    • Initialize the LibGDX managed activity:
    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题