java - 我如何从 libgdx 中的另一个类调用方法

标签 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();
    }
}

最佳答案

在core中创建接口(interface),在AndroidLauncher中实现该接口(interface)并发送给游戏。 因此您可以调用方法或传递数据来渲染。

界面:

    public interface SomeInterface    {
    public void walo();
}

Android启动器:

   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 服务接口(interface))和我的 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/

相关文章:

java - Gradle Drools 6.2 无法解析配置 ':compile' 的所有依赖项

java - 'CREATE TABLE' 数据库中的多语句事务中不允许使用 'tempdb' 命令

java - 连接到 Google Play 游戏时出错

java - 与 Java 的相互认证(带有客户端证书 PFX 文件)并且无法访问环境

java - 使用链表数据结构面对 insertBefore 方法的问题

ruby - 为什么方法声明在 ruby​​ 中评估为 nil?

php - 在 PHP 中保存函数的类变量

C++:在类 w.r.t. 的 vector 上选择 argmax任意表达

c++ - 是否可以允许将类强制转换为任意枚举?

dictionary - 内置 map 方法引用