【问题标题】:Google Play Game Services with libGDX带有 libGDX 的 Google Play 游戏服务
【发布时间】:2016-02-02 18:48:10
【问题描述】:

我已尝试将 Google Play 游戏服务与我的 libGDX 项目集成。

我在 MainGame 类中使用过这段代码:

public static PlayServices playServices;

public MainGame(PlayServices playServices)
{
    this.playServices = playServices;
}

我把这段代码放在 MainMenu 类中,我想在一些用户交互后显示登录 Google 的登录表单。

public static MainGame game;

public MainMenu(MainGame game)
{
    this.game = game;
}

当我尝试运行应用程序时,出现以下错误:

Error:(77, 68) 错误: MainMenu 类中的构造函数 MainMenu 不能 适用于给定类型;必需:找到 MainGame:无参数 原因:实际参数列表和形式参数列表的长度不同

【问题讨论】:

  • 至少给个+1?
  • @ossobuko:当然!你能不能再帮忙一点。我检查了提到的问题,但所有这些都是正确的。教程没有说要创建类,只是给它一些额外的功能。我也可以签名为已解决! :)

标签: libgdx google-play-games


【解决方案1】:

我猜你正试图从 Android 模块向你的核心模块发送一个 PlayServices 的实例。如果不是这种情况,请更正我并提供更多代码,因为我无法根据您提供的信息做出任何其他事情。

您需要使用接口。

在您的核心模块中创建一个接口。其中有connectToGooglePlayServices()、unlockAchivement()等必要的功能。所有的东西都是Android独有的。

public class AndroidLauncher extends AndroidApplication implements ThatInterfaceClass {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new MyGdxGame(this), config);
    }
    @Override
    public void connectToGooglePlayServices() {
        //connect to play services here
    }

    @Override
    public void unlockAchivement() {
        // unlock achivement etc.
    }

}

这里的重要部分是将其作为参数提供给MyGdxGame 类构造函数,并实现ThatInterfaceClass

之后在你的 MyGdxGame 类的构造函数中添加一个参数。

public class MyGdxGame extends ApplicationAdapter {
    public void MyGdxGame (ThatInterfaceClass interface) {
        this.interface = interface;
    }

    // create etc... Other functions....
}

现在要连接到 google play 服务或解锁成就,只需拨打interface.connectToGooglePlayServices()

这里有一个链接可以更好地解释这一点。

https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code

编辑: 问题似乎出在您未在此处提供的代码中。看看这个你应该如何创建的例子 e MainMenu 类的一个实例。

MainGame mainGame = new MainGame(playServices);
MainMenu mainMenu = new MainMenu(mainGame);

我相信你正在构建这样的 MainMenu 类:

MainMenu mainMenu = new MainMenu();

只要你有这个构造函数:

public MainGame(PlayServices playServices)
{
    this.playServices = playServices;
}

你不能使用没有 PlayServices 参数的构造函数。但是您可以删除您的构造函数或删除 PlayServices 参数并像这样使用它:

MainMenu mainMenu = new MainMenu();
mainMenu.setPlayServices(playServices);

这是另一个和你一样的问题:

"Actual or formal argument lists differs in length"

编辑 2: 您的 MainMenu 类是否在 核心模块android 模块 中?如果它不在 android 模块 中,则不能使用 PlayServices 类(也不能有实例)。也不要寻找解决方法,只需使用接口来启动连接。正如我在最初的回答中所说的那样。

编辑 3:最终版:如果您之前已经介绍过本教程,对我们双方来说可能会更容易。 您在 cmets 中发送的图像说 playServices 无法解析:

  1. 确保您没有输入任何错误。

  2. 确保 PlayServices 接口在核心模块中。

  3. 再次阅读教程。确保您正确添加了库、依赖项等。

  4. 本教程告诉您创建 AndroidLauncher 活动。但是 libGdx 默认会创建它。

除此之外,如果不坐在你的椅子上,我无法进一步帮助你。对于您的下一个问题,请提供更多信息。还有很多。

【讨论】:

  • 是的,你是对的!我已经实现了接口(这是PlayServices)和所有必要的功能。我的菜单与您的相似,但是当我尝试从其他课程调用 MainMenu 时会出错。
  • 我已经在另一个类中尝试了另一个类的 MainMenu 类的正确实例。它在这个其他类中给出了这个错误:prntscr.com/9yn8lg
  • MainMenu 在核心项目中,但PlayServices 接口也在核心项目中。此方法基于本教程:chandruscm.wordpress.com/2015/12/30/…
猜你喜欢
  • 1970-01-01
  • 2014-04-17
  • 2016-01-04
  • 2015-05-08
  • 1970-01-01
  • 1970-01-01
  • 2013-07-17
  • 2014-02-02
相关资源
最近更新 更多