其他分享
首页 > 其他分享> > android-邀请玩家离开游戏时调用onDisconnectedFromRoom()

android-邀请玩家离开游戏时调用onDisconnectedFromRoom()

作者:互联网

我与其他2个玩家(都直接邀请了)一起创建了实时多人游戏.这是我的方法:

创建玩家选择意图

Intent intent = Games.RealTimeMultiplayer.getSelectOpponentsIntent(getApiClient(), 1, MAX_PLAYERS);
startActivityForResult(intent, REQ_GOOGLE_INVITE);

在onActivityResult()上执行以下操作:

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

    Bundle autoMatchCriteria = RoomConfig.createAutoMatchCriteria(1, 5, 0);

    // create the activeRoom and specify a variant if appropriate
    RoomConfig.Builder roomConfigBuilder = makeBasicRoomConfigBuilder();
    roomConfigBuilder.addPlayersToInvite(invitees);
    roomConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
    RoomConfig roomConfig = roomConfigBuilder.build();
    Games.RealTimeMultiplayer.create(getApiClient(), roomConfig);

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


private RoomConfig.Builder makeBasicRoomConfigBuilder() {
    return RoomConfig.builder(this).setMessageReceivedListener(this).setRoomStatusUpdateListener(this).setSocketCommunicationEnabled(true)
            .setVariant(subcategory.getId().intValue());
}

我的游戏在所有3台设备上启动,但是当我在一台受邀设备上与游戏断开连接时,在主机设备上调用了onDisconnectedFromRoom(),因此我猜想游戏会自动取消,这很可悲,因为我想继续游戏,直到出现所需的最低人数.

有办法防止这种情况吗?

解决方法:

在onDisconnectedFromRoom()回调中,您将获得Room对象.您可以通过致电查看有多少玩家留在房间里

int count = room.getParticipantIds().size();

如果玩家超过最小人数,则继续游戏.

标签:google-play-games,android
来源: https://codeday.me/bug/20191121/2049918.html