【发布时间】:2015-05-22 13:43:35
【问题描述】:
我正在尝试使用 Play 游戏服务在 Android 中开发一个简单的回合制多人游戏。我遵循了文档中的所有步骤:https://developers.google.com/games/services/android/turnbasedMultiplayer#implementing_auto-matching 唯一的区别是我不希望我的玩家能够邀请朋友我希望它是纯粹的自动匹配。游戏只有 2 名玩家,并且只能在 2 名玩家匹配后开始。我的问题是它似乎没有自动化播放器。我在 2 台设备上运行该应用程序,但它们似乎从未找到彼此……它们都很好地连接到了 Play 游戏服务,但它们都只是创建了一个新游戏。这是我连接 GoogleApiClient 后的代码。
@Override
public void onConnected(Bundle bundle) {
pDialog.dismiss();
Toast.makeText(this, "Connected", Toast.LENGTH_LONG).show();
showDialog("Matching players");
Bundle autoMatchCriteria = RoomConfig.createAutoMatchCriteria(1,1,0);
TurnBasedMatchConfig tbmc=TurnBasedMatchConfig.builder().
setAutoMatchCriteria(autoMatchCriteria).build();
Games.TurnBasedMultiplayer.createMatch(mGoogleApiClient,
tbmc).setResultCallback(new MatchInitiatedCallback(this));
}
这是我的 MatchInitiatedCallback
public class MatchInitiatedCallback implements
ResultCallback<TurnBasedMultiplayer.InitiateMatchResult> {
private Context context;
public MatchInitiatedCallback(Context c) {
context = c;
}
@Override
public void onResult(TurnBasedMultiplayer.InitiateMatchResult
initiateMatchResult) {
pDialog.dismiss();
if(!initiateMatchResult.getStatus().isSuccess()) {
Toast.makeText(context, "ERROR: " +
initiateMatchResult.getStatus().getStatusCode(), Toast.LENGTH_LONG).show();
return;
}
TurnBasedMatch match = initiateMatchResult.getMatch();
if(match.getData() != null) {
Toast.makeText(context, "Player2 " + match.getData().toString(),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Player1", Toast.LENGTH_LONG).show();
initGame(match);
}
}
}
两个设备都显示 TOAST,上面写着:“Player1”并调用这里的 initGame(match) 方法:
public void initGame(TurnBasedMatch match) {
String initialise = "initialised";
Games.TurnBasedMultiplayer.takeTurn(mGoogleApiClient,
match.getMatchId(),initialise.getBytes(Charset.forName("UTF-16")),
match.getParticipantId(Games.Players.getCurrentPlayerId(mGoogleApiClient))).
setResultCallback(this);
}
@Override
public void onResult(TurnBasedMultiplayer.UpdateMatchResult
updateMatchResult) {
if(updateMatchResult.getMatch().getStatus() ==
TurnBasedMatch.MATCH_STATUS_AUTO_MATCHING) {
Toast.makeText(this, "Still automatching",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Not automatching", Toast.LENGTH_LONG).show();
}
}
他们再次显示 TOAST:“仍在自动化” 我究竟做错了什么。为什么设备不自动匹配。我是否在某处跳过了一步。请帮忙。
【问题讨论】:
标签: android google-play-services multiplayer google-play-games