如何从 android 后端调用核心方法?

How to call core method from android backend?

我可以使用来自 GDX(平台特定代码)的 android 方法,但是是否可以从 android 后端获取 libgdx 方法? 我有火力数据库。在游戏的 android 端,我发现了数据库中的任何变化。我需要将更改转移到我的核心后端(例如更新一些演员、标签等)。最好的方法是什么?

好消息,这是可行且简单的,只需导入您需要的任何内容,例如来自 LibGDX

的颜色 class
import com.badlogic.gdx.graphics.Color;

public class AndroidLauncher extends AndroidApplication {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();


    Color color = new Color();
    initialize(new Game(), config);
}

希望这就是您所需要的

使用 Interfacing.

可以访问 core module 内的特定平台 API

core-module 是所有平台的通用部分,因此您可以在项目的任何地方访问。

保留 ApplicationListener 的引用,如果你想 call any method/access data member 你的核心模块。

android 模块内部:

public class AndroidLauncher extends AndroidApplication {

    MyGdxGame gdxGame;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

        gdxGame=new MyGdxGame();
        initialize(gdxGame, config);
    }

     public void andoridMethod(){
         System.out.println(gdxGame.x);      //access data member
         gdxGame.doSomething();              //access method
     }
}

内部核心模块:

public class MyGdxGame implements ApplicationListener {

      public int x=4;

      public void doSomething(){}

      // Life cycle methods of ApplicationListener
}

如果您只是按照 Aryan 的建议从 Android 代码中调用核心函数,代码将在不同的线程上执行,这可能会导致问题,除非您将代码设计为线程安全的。

如果您想确保它在 Gdx 渲染线程上执行,您应该在 Android 代码中保留对您的游戏的引用,然后使用

    Gdx.app.postRunnable(new Runnable(){
        @Override
        public void run(){
            gdxGame.doSomething();
        }
    })

runnable 应在渲染循环开始时执行(在输入处理之前)。