当我调用Games.RealTimeMultiplayer.getWaitingRoomIntent

时间:2016-07-29 04:43:05

标签: android google-play-services google-play-games

我正在尝试学习Google Play游戏服务,而我在使用Games.RealTimeMultiplayer.getWaitingRoomIntent时遇到了问题。它是否应该显示所有被邀请的用户的意图?

我能够显示玩家选择,选择玩家后,我打电话

Intent intent = Games.RealTimeMultiplayer.getWaitingRoomIntent(mGoogleApiClient, room, 1);
        startActivityForResult(intent, RC_WAITING_ROOM);

在onRoomCreated内,并且还显示默认的等待室意图,但仅在创建房间的播放器的设备上显示。但在那之后,我没有收到任何回调中的任何内容。

这是我的代码:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);
    textViewMessages = (TextView) findViewById(R.id.textViewMessages);

    buttonStartMatch = (Button) findViewById(R.id.buttonStartMatch);
    buttonStartMatch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.buttonStartMatch:
                    Intent intent = Games.RealTimeMultiplayer.getSelectOpponentsIntent(mGoogleApiClient, 1, 7, true);
                    startActivityForResult(intent, RC_SELECT_OPPONENT);
                    break;
            }
        }
    });

    // Create the Google Api Client with access to the Play Games services
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API)
            .addScope(Games.SCOPE_GAMES)
            .build();
}



@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}



protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    switch (requestCode) {
        case RC_SIGN_IN:
            mSignInClicked = false;
            mResolvingConnectionFailure = false;
            if (resultCode == RESULT_OK) {
                Log.i("RC_SIGN_IN", "OK");
                mGoogleApiClient.connect();
            } else {
                Log.i("RC_SIGN_IN", "FAILED");
            }
            break;
        case RC_RESOLVE_ERROR:
            if (resultCode == RESULT_OK) {
                Log.i("RC_RESOLVE_ERROR", "OK");
                textView.setText("");
                mGoogleApiClient.connect();
            }
            else {
                Log.i("RC_RESOLVE_ERROR", "FAILED");
            }
            break;
        case RC_SELECT_OPPONENT:
            if (resultCode != Activity.RESULT_OK) {
                // user canceled
                return;
            }

            // get the invitee list
            final ArrayList<String> invitees =
                    data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);

            // get auto-match criteria
            Bundle autoMatchCriteria = null;
            int minAutoMatchPlayers =
                    data.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
            int maxAutoMatchPlayers =
                    data.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);

            if (minAutoMatchPlayers > 0) {
                autoMatchCriteria = RoomConfig.createAutoMatchCriteria(
                        minAutoMatchPlayers, maxAutoMatchPlayers, 0);
            } else {
                autoMatchCriteria = null;
            }

            // create the room and specify a variant if appropriate
            RoomConfig.Builder roomConfigBuilder = makeBasicRoomConfigBuilder();
            roomConfigBuilder.addPlayersToInvite(invitees);
            if (autoMatchCriteria != null) {
                roomConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
            }
            RoomConfig roomConfig = roomConfigBuilder.build();
            Games.RealTimeMultiplayer.create(mGoogleApiClient, roomConfig);

            // prevent screen from sleeping during handshake
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

            break;

        case RC_WAITING_ROOM:
            if (resultCode == RESULT_OK) {
                Log.i("RC_WAITING_ROOM", "START GAME!!!!");
            }
            else if (resultCode == RESULT_CANCELED) {
                Log.i("RC_WAITING_ROOM", "USER CANCELLED");
            }
            else if (resultCode == GamesActivityResultCodes.RESULT_LEFT_ROOM) {
                Log.i("RC_WAITING_ROOM", "USER LEFT THE ROOM");
            }
            break;
    }
}











// create a RoomConfigBuilder that's appropriate for your implementation
private RoomConfig.Builder makeBasicRoomConfigBuilder() {
    return RoomConfig.builder(new OnRoomUpdateListener())
            .setMessageReceivedListener(new OnMessageReceivedListener())
            .setRoomStatusUpdateListener(new OnRoomStatusUpdateListener());
}






private class OnRoomUpdateListener implements RoomUpdateListener {
    @Override
    public void onRoomCreated(int statusCode, Room room) {
        Log.i("ROOM CREATED", room.toString());
        if (statusCode != GamesStatusCodes.STATUS_OK) {
            Log.i("ROOM CREATED ERROR", statusCode + "");
        }

        Intent intent = Games.RealTimeMultiplayer.getWaitingRoomIntent(mGoogleApiClient, room, 1);
        startActivityForResult(intent, RC_WAITING_ROOM);
    }

    @Override
    public void onJoinedRoom(int statusCode, Room room) {
        Log.i("JOINED ROOM", "JOINED ROOM");
        if (statusCode != GamesStatusCodes.STATUS_OK) {
            Log.i("JOINED ROOM ERROR", statusCode + "");
        }

        Intent intent = Games.RealTimeMultiplayer.getWaitingRoomIntent(mGoogleApiClient, room, 1);
        startActivityForResult(intent, RC_WAITING_ROOM);
    }

    @Override
    public void onLeftRoom(int statusCode, String s) {
        Log.i("LEFT ROOM", "LEFT ROOM");
        if (statusCode != GamesStatusCodes.STATUS_OK) {
            Log.i("LEFT ROOM ERROR", statusCode + "");
        }
    }

    @Override
    public void onRoomConnected(int statusCode, Room room) {
        Log.i("ROOM CONNECTED", "ROOM CONNECTED");
        if (statusCode != GamesStatusCodes.STATUS_OK) {
            Log.i("ROOM CONNECTED ERROR", statusCode + "");
        }
    }
}






private class OnRoomStatusUpdateListener implements RoomStatusUpdateListener {

    @Override
    public void onRoomConnecting(Room room) {
        Log.i("ROOM CONNECTING", "ROOM CONNECTING");
    }

    @Override
    public void onRoomAutoMatching(Room room) {
        Log.i("AUTO MATCHING", "AUTO MATCHING");
    }

    @Override
    public void onPeerInvitedToRoom(Room room, List<String> list) {
        Log.i("PEER INVITED", "PEER INVITED");
    }

    @Override
    public void onPeerDeclined(Room room, List<String> list) {
        Log.i("PEER DECLINED", "PEER DECLINED");
    }

    @Override
    public void onPeerJoined(Room room, List<String> list) {
        Log.i("PEER JOINED", "PEER JOINED");
    }

    @Override
    public void onPeerLeft(Room room, List<String> list) {
        Log.i("PEER LEFT", "PEER LEFT");
    }

    @Override
    public void onConnectedToRoom(Room room) {
        Log.i("CONNECTED TO ROOM", "CONNECTED TO ROOM");
    }

    @Override
    public void onDisconnectedFromRoom(Room room) {
        Log.i("DISCONNECTED FROM ROOM", "DISCONNECTED FROM ROOM");
    }

    @Override
    public void onPeersConnected(Room room, List<String> list) {
        Log.i("PEERS CONNECTED", "PEERS CONNECTED");
    }

    @Override
    public void onPeersDisconnected(Room room, List<String> list) {
        Log.i("PEERS DISCONNECTED", "PEERS DISCONNECTED");
    }

    @Override
    public void onP2PConnected(String s) {
        Log.i("P2P CONNECTED", "P2P CONNECTED");
    }

    @Override
    public void onP2PDisconnected(String s) {
        Log.i("P2P DISCONNECTED", "P2P DISCONNECTED");
    }
}





private class OnMessageReceivedListener implements RealTimeMessageReceivedListener {

    @Override
    public void onRealTimeMessageReceived(RealTimeMessage realTimeMessage) {
        Log.i("REAL TIME MESSAGE", realTimeMessage.toString());
    }
}

1 个答案:

答案 0 :(得分:0)

Optional: Adding a waiting room UI中所述,

  

getWaitingRoomIntent()中的第二个参数表示在显示开始播放选项之前必须在房间中连接的播放器数量。在示例中,我们指定MAX_VALUE,表示开始播放选项永远不会显示。相反,当所有玩家都连接时,等候室UI会自动退出。

可以在Adding Real-time Multiplayer Support to Your Android Game中找到更多信息和示例代码,并且通过RealTimeMultiplayer也会有所帮助。