首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Games.RealtimeMultiplayer.getWaitingRoomIntent空指针异常

Games.RealtimeMultiplayer.getWaitingRoomIntent空指针异常
EN

Stack Overflow用户
提问于 2014-02-22 03:15:54
回答 1查看 331关注 0票数 0

我创造了一个房间,它就成功了。我的onRoomCreated方法被调用..。

代码语言:javascript
复制
@Override
    public void onRoomCreated(int statusCode, Room room) {
        mRoomId = room.getRoomId();
        Intent i = Games.RealTimeMultiplayer.getWaitingRoomIntent(gApiClient, room, 2);
        startActivityForResult(i, RC_WAITING_ROOM);
    }

然后在我的onActivityResult..。

代码语言:javascript
复制
Room r = data.getExtras().getParcelable(Multiplayer.EXTRA_ROOM);
ArrayList<String> invitees = new ArrayList<String>();
for (Participant p : r.getParticipants()) {
    invitees.add(p.getPlayer().getPlayerId()); //<---NULL POINTER!
}

我得到了那个空指针。为什么?

编辑: android文档提到了getPlayer()方法..。

返回此参与者表示的播放机。注意,如果播放器的身份未知,则此值可能为空。这发生在一些玩家不允许查看其他玩家的真实身份的自动化场景中。

这就是为什么我要空,因为我的房间是通过自动匹配。

现在的问题是。如何仅使用参与者ID创建一个基于转盘的游戏?不是玩家ID

EN

回答 1

Stack Overflow用户

发布于 2014-02-22 03:58:11

现在我清楚地看到了你的要求(我的错,不是你的错),下面是我是如何做到的:

(为了澄清我使用的是LibGDX,所以可能是一些您不需要的接口内容,而且我仍然在使用GamesClient,而不是新的API方法,但在所有方面都是一样的)

首先,我想开始我的游戏的最后一个电话是onRoomConnected。

代码语言:javascript
复制
@Override
public void onRoomConnected(int statusCode, Room room) {
    //dLog("onRoomConnected");
    mRoomCurrent = room;
    mParticipants = room.getParticipants();

    mMyID = room.getParticipantId(aHelper.getGamesClient().getCurrentPlayerId());
    //dLog("The id is " + mMyID);

    try {
        bWaitRoomDismissedFromCode = true;
        finishActivity(RC_WAITING_ROOM);
    } catch (Exception e) {
        //dLog("would have errored out in waiting room");
    }

    //tell the Game the room is connected
    if (statusCode == GamesClient.STATUS_OK) {
        theGameInterface.onRoomConnected(room.getParticipantIds(), mMyID, room.getCreationTimestamp() );
    } else {
        leaveRoom();
    }

}

所以,现在有所有的参与者So。现在在我的游戏代码(我发送it列表)中,我对it列表进行排序,以便在确定玩家顺序时,对所有玩家都采用相同的方法。首先,我建立我的对手。

代码语言:javascript
复制
private void buildOpponents() {
    // this creates a new opponent with a View on the Stage()

    //sort the participants the same for all players
    sortParticipantIDs();

    for (String s : mParticipantIds) {
        if(s.contains(mMyID) || mMyID.contains(s)) continue;
        newOpponentWindow ow = new newOpponentWindow(s, MyAssetManager.getMySkin(), getStage());
        Opponent o = new Opponent(this, s);
        mapOpponents.put(s, o);
        o.setWindow(ow);
        getStage().addActor(ow);
    }


    setOpponentWindowPositions();


}

然后,经过更多的设置,我开始播放,我的第一次通过,我选择谁是最高的ID获得荣誉开始(我发现这随机播放足够多,而不必做另一种方法。.but你可以让顶部的ID做另一种方法,并发送给其他玩家)注意,这检查了我的对手,以确定是否有人离开房间在游戏的晚些时候。

代码语言:javascript
复制
private boolean determineIfStartingBidder() {
    Collections.sort(mParticipantIds);

    // now look thru list
    // if the number is mine then return true
    // if the number is not mine.. and opponent is not Out of Game or Disconnected.. then return false

    for (String s : mParticipantIds) {
        if(s.contains(mMyID) || mMyID.contains(s)){
            return true;
        }
        if(mapOpponents.get(s).getCurrentState() == currentState.DISCONNECTED || mapOpponents.get(s).getCurrentState()  == currentState.OUTOFGAME  ||
                            mapOpponents.get(s).getCurrentState() == currentState.LOSTGAME) {
            continue;
        }
        return false;
    }

    return false;
}

然后,在你的游戏逻辑中,只要看看你的ParticipantID列表,以任何有意义的方式传递接力棒就可以了!这很好用,因为所有传递消息的调用都需要ParticipantID,并且是为了方便抓取n go!

先前的答案在下面

试一试

代码语言:javascript
复制
data.getParcelableExtra(Multiplayer.EXTRA_ROOM);

不需要getExtras

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21949162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档