决定谁是球员之一

决定谁是球员之一

本文介绍了决定谁是球员之一,两人在一个圆形基础的游戏与谷歌Play游戏服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个圆形为基础的多人游戏为Android已经在工作之前XMPP,我想切换到谷歌Play游戏服务。在旧版本中,有一个XMPP机器人,决定哪些球员将是玩家1或2。这是很重要的知道哪些球员应该做的第一个动作。

I have a round-based Multiplayer game for Android that has been working over XMPP before, and I want to switch to Google Play Game Services. In the old version, there was an XMPP bot, deciding which player will be player 1 or 2. This is important to know which player should make the first move.

随着谷歌Play游戏服务,我发现,近有效的解决方案:

With Google Play Game Services, I found a solution that nearly works:

@Override
public void onRoomConnected(int statusCode, Room room) {
    ArrayList<Participant> participants = room.getParticipants();
    Participant first = participants.get(0);
    if (first.getPlayer() == null || !first.getPlayer().getPlayerId().equals(myPlayerId)) {
        LaskaField.HUMAN_PLAYER = 2;
        LaskaField.OTHER_PLAYER = 1;
        opponent = first.getDisplayName();
    } else {
        LaskaField.HUMAN_PLAYER = 1;
        LaskaField.OTHER_PLAYER = 2;
        opponent = participants.get(1).getDisplayName();
    }
    setPlayerNames();
}

此方式,邀请其他玩家时,工作正常。然而,当双方球员选择自动匹配它往往会失败。在这种情况下,双方球员都在参加ArrayList中的相同位置。因此,他们都将显示为他们当前使用的设备上的同一个球员。

This way works fine when inviting another player. However, it often fails when both players select auto-matching. In this case, both players are on the same position in the participants ArrayList. Therefore, they will both appear as the same player on their currently used device.

这是选择播放器1和2,具有用于决定此没有中央实例的正确方式。是否有与会者列表中的任何有用的数据,我没有使用调试器找到?

What is the right way to select player 1 and 2, with no central instance for deciding this. Is there any useful data in the participants list that I didn't find with the debugger?

推荐答案

我通过对比玩家ID(这是随机的每场比赛)解决了这个问题:

I have solved the problem by comparing the player ids (which are random for each game):

String myid = mActiveRoom.getParticipantId(client.getCurrentPlayerId());
String remoteId = null;

ArrayList<String> ids = mActiveRoom.getParticipantIds();
for(int i=0; i<ids.size(); i++)
{
    String test = ids.get(i);
    if( !test.equals(myid) )
    {
        remoteId = test;
        break;
    }
}

boolean iMakeTheFirstMove = ( myid.compareTo(remoteId) > 0 );

这篇关于决定谁是球员之一,两人在一个圆形基础的游戏与谷歌Play游戏服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 12:19