2015-07-03 29 views
4

我正在使用libgdx。我需要將TaskSet的ArrayList從android傳遞給core。問題在於TaskSet位於android模塊中。我可以通過一些標準的對象,如字符串是這樣的:從android模塊傳遞ArrayList到libgdx中的核心

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放置到「核心」模塊,但無論如何,我需要一些方法來解決Android部分

回答

2

如果你以你問的方式因此,您將無法維護多平臺功能。這也意味着你將無法在桌面上進行測試。這會花費您很多時間在設備上編譯和加載Android APK。

但是,您應該可以將android塊中的所有內容剪切並粘貼到項目的build.gradle文件中的core塊中。它看起來像這樣:

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); 
    } 
} 
+0

感謝。我很欣賞 –