Libgdx 游戏 Google 在 android 应用程序问题中玩游戏服务

Libgdx game with Google play game services in android app problems

我一直在询问与我的 Libgdx 游戏 Google 玩游戏服务配置错误相关的问题。到目前为止,我已经解决了登录错误,但现在我被困在解锁成就上。所以我正在 post 编写我的代码,也许有人可以帮助我。

这是我在 Core Libgdx 项目

中创建的 ActionResolver 接口
  package com.------.game;

    public interface ActionResolver {
    public boolean getSignedInGPGS();
    public void loginGPGS();
    public void submitScoreGPGS(int score);
    public void unlockAchievementGPGS(String achievementId);
    public void getLeaderboardGPGS();
    public void getAchievementsGPGS();
    public void onShowAchievementsRequested() ;
    }

我的 Android 启动器 class 是

 package com.------.game.android;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import android.widget.Toast;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;

import com.google.android.gms.games.Games;
import com.google.android.gms.plus.Plus;


import com.google.example.games.basegameutils.BaseGameUtils;
import com.google.example.games.basegameutils.GameHelper;
import com.google.example.games.basegameutils.GameHelper.GameHelperListener;


import com.-----.game.ActionResolver;
import com.-----.game.MainGame;

public class AndroidLauncher extends AndroidApplication implements
ActionResolver, GameHelperListener ,  GoogleApiClient.ConnectionCallbacks, 
GoogleApiClient.OnConnectionFailedListener 
{



    private GameHelper gameHelper;
    private GoogleApiClient client;

    private Exception e;
     final String TAG = "TanC";
     private boolean mResolvingConnectionFailure = false;


     // Has the user clicked the sign-in button?
     private boolean mSignInClicked = false;
     // Automatically start the sign-in flow when the Activity starts
     private boolean mAutoStartSignInFlow = true;
     // request codes we use when invoking an external activity
//   private static final int RC_RESOLVE = 5000;
     private static final int RC_UNUSED = 5001;
     private static final int RC_SIGN_IN = 9001;
     // tag for debug logging
     final boolean ENABLE_DEBUG = true;

     // playing on hard mode?
     boolean mHardMode = false;
     private int Score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Score= 100;

        client = new GoogleApiClient.Builder(this)
          .addConnectionCallbacks(this)
          .addOnConnectionFailedListener(this)
          .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
          .addApi(Games.API).addScope(Games.SCOPE_GAMES)
          .build();


GameHelper.GameHelperListener gameHelperListener = new GameHelper.GameHelperListener() {
    @Override
    public void onSignInFailed() {
        Log.i("Game Helper", "Sign in failed");
    }

    @Override
    public void onSignInSucceeded() {
        Log.i("Game Helper", "Sign in succeeded");
    }
};

            if (gameHelper == null) {
                gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
                gameHelper.enableDebugLog(true);
                }



        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new MainGame(this), config); // or initialize (game,
                                                        // config);
        // gameHelper.setPlusApiOptions(PlusOptions.builder().build());
        // no title is needed


        gameHelper.setup(gameHelperListener );
    //  gameHelper.setup(gameHelperListener );

    }

    @Override
    public void onStart() {
        super.onStart();

        gameHelper.onStart(this);
          client.connect();

    }

    @Override
    public void onStop() {
        super.onStop();
        // ...

        gameHelper.onStop();
         if (client.isConnected()) {

             client.disconnect();
            }





    }




    @Override
    public void loginGPGS() {
        try {
            runOnUiThread(new Runnable() {
                public void run() {
                    gameHelper.beginUserInitiatedSignIn();
                }
            });
        } catch (final Exception ex) {
             e.printStackTrace ();
        }
    }

    @Override
    public void unlockAchievementGPGS(String achievementId) {
     if ( getSignedInGPGS()) {

        if(Score>=100){

    unlockAchievementGPGS("ABC-------");



        }


         Games.Achievements.unlock(client, getString(R.string.achievement_Trekker));
     } 

    }



    @Override
    public void getLeaderboardGPGS() {

    }

    @Override
    public void getAchievementsGPGS() {
        if (gameHelper.isSignedIn()) {
            startActivityForResult(
                    Games.Achievements.getAchievementsIntent(gameHelper
                            .getApiClient()), 101);
        } else if (!gameHelper.isConnecting()) {
            loginGPGS();
        }

    }



    @Override
    public void submitScoreGPGS( int score) {


        submitScoreGPGS(Score);
    //   game.actionResolver.submitScoreGPGS(world.score);



    }







    @Override
    public void onActivityResult(int request, int response, Intent data) {
        super.onActivityResult(request, response, data);
        gameHelper.onActivityResult(request, response, data);
    }

    @Override
    public boolean getSignedInGPGS() {
        return gameHelper.isSignedIn();
    }

     private boolean isSignedIn() {
            return (client!= null && client.isConnected());
        }


      @Override
        public void onShowAchievementsRequested() {
            if (isSignedIn()) {
                startActivityForResult(Games.Achievements.getAchievementsIntent(client),
                        RC_UNUSED);
            } else {
                BaseGameUtils.makeSimpleDialog(this, getString(R.string.achievement_Trekker)).show();
            }
        }





    @Override
    public void onSignInFailed() {
    }

    @Override
    public void onSignInSucceeded() {
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {

         Log.d(TAG, "onConnectionFailed(): attempting to resolve");
            /*   if (mResolvingConnectionFailure) {
                 Log.d(TAG, "onConnectionFailed(): already resolving");
                 return;
                 }
                 if (mSignInClicked || mAutoStartSignInFlow) {
                 mAutoStartSignInFlow = false;
                 mSignInClicked = false;
                 mResolvingConnectionFailure = true;
                 if (!BaseGameUtils.resolveConnectionFailure(this, client, connectionResult, 
                         RC_SIGN_IN, getString(R.string.unknown_error))) {
                 mResolvingConnectionFailure = false;
                 }*/


    }

    @Override
    public void onConnected(Bundle arg0) {
          Log.i("Google API", "onConnected(): connected to Google APIs");

    }

    @Override
    public void onConnectionSuspended(int arg0) {
         Log.d(TAG, "onConnectionSuspended(): attempting to connect");
         client.connect();


    }

}

这个数值为 100 的 score int 我只是想测试它是否有助于解锁成就。

我在 Core 项目中的 MainGame class 是

   package com.----------.game;

import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import com.-------.helpers.AssetLoader;

import com.------.screens.FirstSplash;

public class MainGame extends Game {

    SpriteBatch batch;
    Texture img;
    public ActionResolver actionResolver;

    Game game;


    public MainGame(ActionResolver actionresolver) {

        this.actionResolver = actionresolver;

    }

    @Override
    public void create() {

        Gdx.app.log("Game", "created");
        AssetLoader.load();

        setScreen(new FirstSplash(this));

    }

    public void show() {
        Gdx.app.log("my Splash Screen", "show called");


        if (Gdx.app.getType() == ApplicationType.Android) {
            actionResolver.getSignedInGPGS();

             actionResolver.submitScoreGPGS(110);

             actionResolver.unlockAchievementGPGS("ABC-----");



        } else {
            actionResolver.loginGPGS();
        }

    }

    @Override
    public void dispose() {

        super.dispose();
        AssetLoader.dispose();
    }

}

我的 Android 清单文件是

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.--------.game.android"
    android:versionCode="2"
    android:versionName="1.1" >

    <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="20" />


    <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />




    <application
        android:allowBackup="true"
        android:icon="@drawable/smallicon"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme" 
        android:name="com.---------.game.android.MyApplication">



<meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id" />

   <meta-data android:name="com.google.android.gms.version"
       android:value="@integer/google_play_services_version"/>



        <activity
            android:name="com.outofboxapps.game.android.AndroidLauncher"
            android:label="@string/app_name" 
            android:screenOrientation="landscape"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>



    </application>

</manifest>

我在 Android/Res 文件夹中的字符串文件有这个

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">My Game</string>

    <string name="app_id">------------</string> // has my app id
</resources>

我的 Logcat 当我 运行 我的应用程序处于调试模式时会这样说(注意:我已经在开发者控制台上配置 Google 玩游戏服务| 我的游戏已经发布,没有第一版的游戏服务,以便测试游戏服务我已经在 Alpha 中发布了我的游戏服务。我已经成功添加了测试人员。我可以使用测试帐户登录。我也有 Dubug 和发布证书客户端 ID)

03-11 10:28:49.823: I/my Spash Screen(16750): constructor called
03-11 10:28:49.829: I/my Splash Screen(16750): hide called
03-11 10:28:49.830: I/my Splash Screen(16750): rendered 2 times.
03-11 10:28:49.830: I/my Splash Screen(16750): show called
03-11 10:28:50.030: D/GameHelper(16750): GameHelper: onConnected: connected!
03-11 10:28:50.038: D/GameHelper(16750): GameHelper: succeedSignIn
03-11 10:28:50.044: D/GameHelper(16750): GameHelper: Notifying LISTENER of sign-in SUCCESS
03-11 10:28:52.837: I/my Spash Screen(16750): constructor called
03-11 10:28:52.869: I/my Splash Screen(16750): hide called
03-11 10:28:52.869: I/my Splash Screen(16750): rendered 180 times.
03-11 10:28:57.361: I/GameScreen(16750): show called
03-11 10:28:57.361: I/GameScreen(16750): resizing

现在我不明白了。 我已成功连接 为什么我不能在游戏服务中做任何其他事情。我只有在特定分数上需要解锁的成就。

使用所有LIBGDX游戏教程,Google键入数字和繁琐的示例和教程我现在感到绝望,因为我根本无法配置这些游戏服务。

我还在游戏结束时从游戏屏幕 class 发送我的分数,但现在删除了它,因为它只是在登录时生成空指针异常(到 google 游戏服务)。我也可以post那个代码

if (Gameover ----) {



        if ((game.actionResolver.getSignedInGPGS())) { // My app crashes at this point ton check Sign in by giving Null pointer exception
            game.actionResolver.submitScoreGPGS(AssetLoader.getScore);
        if (AssetLoader.getScore >= 2500) game.actionResolver.unlockAchievementGPGS("-----"); // my id is here



        }


    } 

请成功完成此操作的任何人。帮帮我。

看看启动器中的 loginGPGS() 方法 class...您可以成功连接,因为 loginGPGS 在 ui 线程上 运行...尝试在 ui 线程上访问 GPGS,因为它需要在 Main activity 的上下文中是 运行。这就是我访问我的方式:

@Override
public void getLeaderboardGPGS() {

    try {
        runOnUiThread(new Runnable() {
            public void run() {
                if (gameHelper.isSignedIn()) {
                    startActivityForResult(
                            Games.Leaderboards.getLeaderboardIntent(
                                    gameHelper.getApiClient(),
                                    "XXXXXXXXXX-XXX-XXX"), 100);
                } else if (!gameHelper.isConnecting()) {
                    loginGPGS();
                }
            }
        });
    } catch (final Exception ex) {
    }

}


@Override
public void sendScoreGPGS() {

    try {
        runOnUiThread(new Runnable() {
            public void run() {
                if (gameHelper.isSignedIn()) {
                    startActivityForResult(
                            Games.Leaderboards.submitScore(gameHelper.getApiClient(),
            "YOUR-GPGS-HASH", score);
                } else if (!gameHelper.isConnecting()) {
                    loginGPGS();
                }
            }
        });
    } catch (final Exception ex) {
    }

}