LIBGDX-访问Android程序包变量 [英] LIBGDX - Accessing Android package variables

查看:43
本文介绍了LIBGDX-访问Android程序包变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过Core包中的类读取Android包中AndroidLauncher中某些变量的值. 如下所示,我需要读取插页式变量的值.

I need to read the value of some variables that are in AndroidLauncher on Android package through a class in Core package. As below, I need to read the value of interstitial variable.

Android上的AndroidLaucher:

AndroidLaucher on Android:

public static int interstital=0;

public int ads() {
    // TODO Auto-generated method stub
    return interstital;
}

Core上的GameScreen:

GameScreen on Core:

System.out.println(AndroidLaucher.ads());

我试图实现一个接口,但是它不支持静态方法. 如何在AndroidLauncher上将此变量的值返回给Core上的GameScreen?

I tried to implement an interface, but it does not support static methods. How to return the value of this variable on AndroidLauncher to GameScreen on Core?

谢谢

推荐答案

不要使用静态方法,尤其是在android上,请创建如下所示的Ads接口:

Don't use static methods, especially on android, create an Ads interface like this:

public interface AdsManager {
    public void showAds();
    public Integer getInterstital();
}

对其进行android实现:

Make an android implementation of it:

public class AndroidAdsManager implements AdsManager{

    private Integer interstital;

    public AndroidAdsManager(){  
        interstital = 0;
    }

    @Override
    public void showAds(){
       //Show ads method
    }
    @Override
    public Integer getInterstital(){
        return interstital;
    }
}

现在在您的android启动器上,您创建实例并将Java引用传递给您的游戏:

now on your android launcher you create the instance and pass the java reference to your game:

public class AndroidLauncher extends AndroidApplication {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        AdsManager androidManager = new AndroidAdsManager();
        initialize(new MyGame(androidManager), config);
    }
}    

最后是您的游戏课:

public class MyGame extends ApplicationAdapter {
    private AdsManager adsManager;
    public Mygame(AdsManager adsManager){
        this.adsManager = adsManager;
    }
}

现在,您可以在代码的任何位置使用adsManager,如果它是在桌面,android或ios上实例化的,它将使用实现中的方法.

Now you can use your adsManager anywhere on your code, it will use the methods from the implementations if it was instanced on desktop, android or ios.

此外,如注释中所述:使用LibGDX时,您想从特定平台获取信息或控制组件,请使用接口.请注意,可能需要对其进行设计,使其可在所有平台上正常工作平台,即使他们不使用广告."

Also, as mentionned in the comments: "When using LibGDX and you want to get information or control components from specific platforms, use interfaces. Just be mindful that you may need to design it so that it works on all platforms even if they don't use ads."

这篇关于LIBGDX-访问Android程序包变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆