我想从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();
    }
}

最佳答案

在核心中创建接口,在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

关于java - 我如何从libgdx中的另一个类调用方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46549180/

10-11 08:31