Google Play 多人游戏:与其他玩家没有连接
Posted
技术标签:
【中文标题】Google Play 多人游戏:与其他玩家没有连接【英文标题】:Google Play Multiplayer: No connection to other players 【发布时间】:2015-10-03 15:15:09 【问题描述】:我目前正在使用 Google Play 多人游戏服务开发游戏。一切正常,例如登录 Google+、排行榜和成就。现在我尝试添加多人游戏功能(通过 Google Play 游戏网络)。我的代码没有抛出任何异常,只是不起作用。我的应用程序中唯一的 logcat
输出是“已创建”(private void startQuickGame ()
)。
public class GameExplorerQuickGame extends Activity implements RoomUpdateListener, RoomStatusUpdateListener
private GoogleApiClient client = RXTXChooser.mGoogleApiClient;
private Room room;
final static int RC_WAITING_ROOM = 10002;
@Override
public void onCreate (Bundle b)
super.onCreate(b);
if (!this.client.isConnected()) this.client.connect();
startQuickGame();
private void startQuickGame ()
Bundle am = RoomConfig.createAutoMatchCriteria(1, 1, 0);
RoomConfig.Builder roomConfigBuilder = makeBasicRoomConfigBuilder();
roomConfigBuilder.setAutoMatchCriteria(am);
RoomConfig roomConfig = roomConfigBuilder.build();
Games.RealTimeMultiplayer.create(this.client, roomConfig);
System.out.println("created"); // printed into logcat
// create a RoomConfigBuilder that's appropriate for your implementation
private RoomConfig.Builder makeBasicRoomConfigBuilder ()
return RoomConfig.builder(this).setMessageReceivedListener(this.googleRxTx).setRoomStatusUpdateListener(this);
@Override
public void onRoomConnected (int statusCode, Room room)
this.room = room;
System.out.println("onRoomConnected");
if (statusCode != GamesStatusCodes.STATUS_OK)
else
//starting Game Activity...
@Override
public void onJoinedRoom (int statusCode, Room room)
this.room = room;
System.out.println("onJoinedRoom");
if (statusCode != GamesStatusCodes.STATUS_OK)
return;
Intent i = Games.RealTimeMultiplayer.getWaitingRoomIntent(this.client, room, Integer.MAX_VALUE);
startActivityForResult(i, RC_WAITING_ROOM);
@Override
public void onRoomCreated (int statusCode, Room room)
this.room = room;
System.out.println("onRoomCreated()");
if (statusCode != GamesStatusCodes.STATUS_OK)
return;
// get waiting room intent
Intent i = Games.RealTimeMultiplayer.getWaitingRoomIntent(this.client, room, Integer.MAX_VALUE);
startActivityForResult(i, RC_WAITING_ROOM);
@Override
public void onActivityResult (int request, int response, Intent intent)
if (request == RC_WAITING_ROOM)
if (response == Activity.RESULT_OK)
//starting Game Activity...
else if (response == Activity.RESULT_CANCELED)
Games.RealTimeMultiplayer.leave(this.client, null, this.room.getRoomId());
else if (response == GamesActivityResultCodes.RESULT_LEFT_ROOM)
Games.RealTimeMultiplayer.leave(this.client, null, this.room.getRoomId());
@Override
public void onConnectedToRoom (Room arg0)
System.out.println("onConnectedToRoom");
this.room = arg0;
@Override
public void onDisconnectedFromRoom (Room room)
this.room = room;
System.out.println("onDisconnectedFromRoom");
Games.RealTimeMultiplayer.leave(this.client, null, room.getRoomId());
// are we already playing?
boolean mPlaying = false;
final static int MIN_PLAYERS = 2;
// returns whether there are enough players to start the game
boolean shouldStartGame (Room room)
int connectedPlayers = 0;
for (Participant p: room.getParticipants())
if (p.isConnectedToRoom()) ++connectedPlayers;
return connectedPlayers >= MIN_PLAYERS;
// Returns whether the room is in a state where the game should be canceled.
boolean shouldCancelGame (Room room)
for (Participant p: room.getParticipants())
// if (!p.isConnectedToRoom()) return true;
return false;
@Override
public void onPeersConnected (Room room, List<String> peers)
System.out.println("onPeersConnected. List.size= " + peers.size());
if (this.mPlaying)
else if (shouldStartGame(room))
@Override
public void onPeersDisconnected (Room room, List<String> peers)
this.room = room;
System.out.println("onPeersDisconnected");
if (this.mPlaying)
else if (shouldCancelGame(room))
Games.RealTimeMultiplayer.leave(this.client, null, this.room.getRoomId());
@Override
public void onPeerLeft (Room room, List<String> peers)
this.room = room;
System.out.println("onPeerLeft");
if (!this.mPlaying && shouldCancelGame(room))
Games.RealTimeMultiplayer.leave(this.client, null, this.room.getRoomId());
@Override
public void onPeerDeclined (Room room, List<String> peers)
this.room = room;
if (!this.mPlaying && shouldCancelGame(room))
Games.RealTimeMultiplayer.leave(this.client, null, this.room.getRoomId());
@Override
public void onP2PConnected (String arg0)
System.out.println("onP2PConnected");
@Override
public void onP2PDisconnected (String arg0)
System.out.println("onP2PDisconnected");
@Override
public void onPeerInvitedToRoom (Room arg0, List<String> arg1)
System.out.println("onPeerInvitedRoom");
this.room = arg0;
@Override
public void onPeerJoined (Room arg0, List<String> arg1)
System.out.println("onPeerJoined");
this.room = arg0;
@Override
public void onRoomAutoMatching (Room arg0)
System.out.println("onRoomAutoMatching");
this.room = arg0;
@Override
public void onRoomConnecting (Room arg0)
System.out.println("onRoomCeonnecting");
this.room = arg0;
@Override
public void onLeftRoom (int arg0, String arg1)
System.out.println("onLeftRoom");
【问题讨论】:
为了帮助解决您的问题,需要阅读大量代码。你能把你的代码缩小到有问题的区域吗? 我试过了。但我不确定问题的根源在哪里...... 【参考方案1】:我找到了解决方案!
我忘了在线添加:
private void startQuickGame ()
//skipping some lines...
RoomConfig.Builder roomConfigBuilder = makeBasicRoomConfigBuilder();
roomConfigBuilder.setAutoMatchCriteria(am);
roomConfigBuilder.setRoomStatusUpdateListener(this); //I forgot this line
RoomConfig roomConfig = roomConfigBuilder.build();
//skipping some lines...
googleAPIClient 因某种奇怪的原因被注销
【讨论】:
以上是关于Google Play 多人游戏:与其他玩家没有连接的主要内容,如果未能解决你的问题,请参考以下文章
当应用程序进入后台时,实时多人 Google Play 游戏服务对等方断开连接
带有 Google Play 游戏服务和 IntentService 的回合制安卓游戏