我正在关注this guide on google设置适用于Android的Google Play游戏服务。
在实现ResultCallback的部分,它说:
在代码段中,MatchInitiatedCallback是一个实现ResultCallback接口的类。您可以将此对象附加到GoogleApiClient,以便在启动匹配时通知您的游戏。要查看MatchInitiatedCallback的实现方式,请参阅第一轮。
public class MatchInitiatedCallback implements
ResultCallback<TurnBasedMultiplayer.InitiateMatchResult> {
@Override
public void onResult(TurnBasedMultiplayer.InitiateMatchResult result) {
// Check if the status code is not success.
Status status = result.getStatus();
if (status.isSuccess()) {
showError(status.getStatusCode());
return;
}
TurnBasedMatch match = result.getMatch();
// If this player is not the first player in this match, continue.
if (match.getData() != null) {
showTurnUI(match);
return;
}
// Otherwise, this is the first player. Initialize the game state.
initGame(match);
// Let the player take the first turn
showTurnUI(match);
}
}
我创建了一个名为&#34; MatchInitiatedCallback&#34;的类。但我不知道该怎么做。我想刚开始一个新的意图。
// Let the player take the first turn
showTurnUI(match);
我尝试在showTurnUI()上开始一个新的意图,但是这个MatchInitiatedCallback不会让我开始一个新的意图,因为它不会从Activity ...(?)
我不明白这些回调的事情,知道从哪里开始,有人能指出我正确的方向吗?
答案 0 :(得分:2)
在GitHub中查看基于回合的示例:https://github.com/playgameservices/android-basic-samples/blob/master/BasicSamples/SkeletonTbmp/src/main/java/com/google/example/tbmpskeleton
使用在主活动上调用方法的匿名类来实现回调。
ResultCallback<TurnBasedMultiplayer.InitiateMatchResult> cb = new ResultCallback<TurnBasedMultiplayer.InitiateMatchResult>() {
@Override
public void onResult(TurnBasedMultiplayer.InitiateMatchResult result) {
processResult(result);
}
};
Games.TurnBasedMultiplayer.createMatch(mGoogleApiClient, tbmc).setResultCallback(cb);
可以在示例的上下文中查看processResult的代码:https://github.com/playgameservices/android-basic-samples/blob/master/BasicSamples/SkeletonTbmp/src/main/java/com/google/example/tbmpskeleton/SkeletonActivity.java#L638