我怎样才能从libgdx中的另一个类调用一个方法

时间:2017-10-03 16:20:44

标签: java class methods libgdx

我想从classMyGdxGame中的render()调用方法walo():

public class MyGdxGame extends ApplicationAdapter{
    public void render() {
        walo();
    }
} 

public class AndroidLauncher extends AndroidApplication {
    AndroidLauncher android =new AndroidLauncher();

    public void walo() {
        Toast.makeText(getApplicationContext(), "You Don't Have Enough Money",
          Toast.LENGTH_LONG).show();
    }
}

1 个答案:

答案 0 :(得分:1)

在核心中创建界面,在AndroidLauncher中实现此界面并将其发送到游戏。 因此,您可以调用方法或传递数据进行渲染。

界面:

    public interface SomeInterface    {
    public void walo();
}

AndroidLauncher:

   public class AndroidLauncher implements SomeInterface{

        @Override
        protected void onCreate() {    

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new MyGdxGame(this), config);

    }

        public void walo() {
          Toast.makeText(getApplicationContext(), "You Don't Have Enough Money",
          Toast.LENGTH_LONG).show();
    }
    }

在游戏课程中

  public MyGdxgame(SomeInterface myinterface)           {
            this.myinterface=myinterface;
        }
   public render()            {
            myinterface.walo()
        }

此处的示例(我的Google Play服务界面)以及指向我的libgdx游戏的开源数据的链接。

    public interface PlayServices
{
    public void signIn();
    public void signOut();
    public void rateGame();
    public void unlockAchievement(String str);
    public void submitScore(int highScore);
    public void submitLevel(int highLevel);
    public void showAchievement();
    public void showScore();
    public void showLevel();
    public boolean isSignedIn();
    public void showBannerAd();
    public void hideBannerAd();
    public void showInterstitialAd (Runnable then);
    public void showRewardedVideo();
    public boolean isRewardEarned();
}

您可以看到这样实施的广告和视频奖励。

Github Connect Game