gpt4 book ai didi

android - Libgdx game with Google play game services in android app问题

转载 作者:行者123 更新时间:2023-11-29 17:40:30 38 4
gpt4 key购买 nike

我一直在询问有关我的 Libgdx 游戏 Google Play 游戏服务配置错误的问题。到目前为止,我已经解决了登录错误,但现在我被困在解锁成就上。所以我发布我的代码可能有人可以帮助我。

这是我在 Core Libgdx 项目中创建的 ActionResolver 接口(interface)

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

我的 AndroidLauncher 类是

 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 类是

   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 list 文件是

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

当我在 Debug模式下运行我的应用程序时,我的 Logcat 会这样说(注意:我已经在开发者控制台上配置了 Google Play 游戏服务|我的游戏已经在第一版没有游戏服务的情况下发布,所以为了测试游戏服务我已经发布了我的游戏服务在Alpha,我已经成功添加测试人员,我可以用测试账号登录,我也有Dubug和发布证书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

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

使用所有 LIBGDX 游戏教程,Google Type a number 和 trival exmaple and tutorial 我现在感到绝望,因为我根本无法配置这些游戏服务。

我也在游戏结束时从 gamescreen 类发送我的分数,但现在删除了它,因为它只是在登录时生成空指针异常(到谷歌游戏服务)。我也可以发布该代码

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() 方法...您可以成功连接,因为 loginGPGS 在 ui 线程上运行...尝试在 ui 线程上访问 GPGS,因为它需要在您的主要 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) {
}

}

关于android - Libgdx game with Google play game services in android app问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28982839/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com