将androidList中的ArrayList传递给libgdx中的core

时间:2015-07-03 12:09:10

标签: java android libgdx

我正在使用libgdx。我需要将TaskSet的ArrayList从android传递给core。问题是TaskSet位于android模块中。我可以这样传递像Strings这样的标准对象:

public class DragAndDropTest extends ApplicationAdapter {
......
    public DragAndDropTest(String value){
        this.value=value;
    }
......
}

在AndroidLauncher中:

AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
                LinearLayout lg=(LinearLayout) findViewById(R.id.game);
                lg.addView(initializeForView(new DragAndDropTest("Some String"), config));

它工作正常,但我需要传递TaskSet的ArrayList,TaskSet是在android模块中

我知道错误的解决办法是将TaskSet放到“核心”模块中,但无论如何我需要一些方法来交互wigh android部分

1 个答案:

答案 0 :(得分:2)

如果按照您要求的方式执行此操作,您将无法维护多平台功能。这也意味着您将无法在桌面上进行测试。这将花费您大量时间来编译和加载Android APK到设备上。

但是你应该能够通过剪切和粘贴项目android文件中core块到build.gradle块的所有内容来实现。它看起来像这样:

project(":core") {
    apply plugin: "java"
    apply plugin: "android"

    configurations { natives }

    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"        
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
    }
}

但就像我说的那样,这可能不是你想要做的。我建议使用一个接口,以便处理TaskSets的所有代码都保留在Android模块中。像这样:

public interface PlatformResolver {
    public void handleTasks();
}

-

public class MyGame extends ApplicationAdapter {
    //......

    PlatformResolver platformResolver;

    public MyGame (PlatformResolver platformResolver){
        this.platformResolver = platformResolver;
    }

    //.....
    public void render(){
        //...

        if (shouldHandleTasks) platformResolver.handleTasks();

        //...
}

-

public class AndroidLauncher extends AndroidApplication implements PlatformResolver {

    public void handleTasks(){
        //Do stuff with TaskSets
    }

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        someDataType SomeData;

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        // config stuff
        initialize(new MyGame(this), config);
    }

}

-

public class DesktopLauncher  implements PlatformResolver{

    public void handleTasks(){
        Gdx.app.log("Desktop", "Would handle tasks now.");
    } 

    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.title = "My GDX Game";
        config.width = 480;
        config.height = 800;
        new LwjglApplication(new MyGame(this), config);
    }
}