我正在使用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部分
最佳答案 如果按照要求的方式执行此操作,则无法维护多平台功能.这也意味着您将无法在桌面上进行测试.这将花费你很多时间来编译和加载Android APK到设备上.
但是你应该能够通过将所有东西从android块切换并粘贴到项目的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);
}
}