How to call core method from android backend?

China☆狼群 提交于 2019-12-23 03:06:19

问题


I can use android methods from GDX (Platform specific code), but is it possible to get libgdx method from android back-end? I have firebase database. On android side of my game I catch any changes in database. And I need to transfer that changes to my core back-end (For example update some actors, labels, and so on). What's the best way to do that?


回答1:


Accessing Platform Specific API inside core module can be possible using Interfacing.


core-module is common part of all platform so you can access anywhere in your project.

Keep reference of ApplicationListener, If you want to call any method/access data member of your core module.

Inside android module :

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
     }
}

Inside core module :

public class MyGdxGame implements ApplicationListener {

      public int x=4;

      public void doSomething(){}

      // Life cycle methods of ApplicationListener
}



回答2:


Good news this is possible and simple, just import whatever you need, for example the Color class from LibGDX

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

Hope this is what you needed




回答3:


If you simply call core functions from your Android code, as suggested by Aryan, the code will be executed on a different thread, which may cause issues unless you have designed your code to be thread safe.

If you want to make sure it is executed on the Gdx render thread, you should keep a reference to your game in the Android code then use

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

The runnable should then be executed at the start of the render loop (before input processing).



来源:https://stackoverflow.com/questions/51431656/how-to-call-core-method-from-android-backend

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!