简体   繁体   中英

Libgdx game with Google play game services in android app problems

I have been asking questions related to my Libgdx game Google play game services configuration error. Up-till now I have solved sign in errors but Now I am stuck at Unlock Achievements. So I am posting my code may Be some one can help me out then.

Here Is my ActionResolver Interface That I created In Core Libgdx project

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

My AndroidLauncher class is

 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();


    }

}

This score int that has a value of 100 I just tried to test if it helps to unlock achievement.

My MainGame class in Core project is

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

}

My Android Manifest File is

 <?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>

My Strings File in Android/Res folder has this

<?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>

My Logcat says this when I run my app in Debug Mode(Note: I have already configured Google play game services on Developer console| My game is already published without Game services in First Version so to test Game services I have published my Game services in Alpha. I have successfully added testers. I am able to sign in with test account. I also have Dubug plus release certificates Client 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

Now I dont get it. when I am SUCCESSFULLY CONNECTED why i cant do anything else in game services. I just have achievements that I need to unlock on a particular score.

Using all LIBGDX game tutorials, Google Type a number and trival exmaple and tutorial I feel hopeless now that I cant configure these game services at all.

I was also sending my scores at game over from gamescreen class but removed it now because it simply generate Null pointer exception on Sign in(to google game services). I can post that code as well

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



        }


    } 

KINDLY ANYBODY WHO HAS DONE THIS SUCCESSFULLY. hELP ME OUT.

Take a look at your loginGPGS() method in your launcher class...You can successfully connect because loginGPGS is run on the ui thread....Try accessing GPGS on the ui thread because it needs to be run in the context of your Main activity....This is how I access mine:

@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) {
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM