Google Play多人游戏切换到游戏画面Libgdx

时间:2014-03-19 18:23:06

标签: android google-play-services multiplayer

我正在使用Libgdx来管理屏幕,更新和渲染。

当我从WAITING_ROOM获得RESULT_OK时,我启动游戏界面,会发生以下情况。

  1. 在候诊室,我可以看到谁发​​起了游戏室
  2. 我可以看到加入等候室的自动匹配的玩家
  3. 当MIN玩家进入房间时,我得到"准备玩#34;
  4. 屏幕切换到游戏屏幕
  5. 现在问题。

    当新游戏画面开始时,其中一个玩家正在显示而另一个玩家有一个空白屏幕 有时,空白屏幕将显示在发起游戏的玩家的手机上,而其他时间则显示在加入房间的玩家的手机上。

    我做错了什么?

    请参阅我的代码:

    ON CREATE

      @Override
       public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        gameActivity = new GameActivity(this, this);
        participants = new Array<Player>();
    
        layout = new RelativeLayout(this);
    
       requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    
         gameView  = initializeForView(gameActivity);
    
          AdRequest adRequest = new AdRequest.Builder()
            .build();
        adView = new AdView(this);
        adView.setAdSize(AdSize.FULL_BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        adView.loadAd(adRequest);
    
        RelativeLayout.LayoutParams gameParams = 
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
          gameParams.addRule(RelativeLayout.CENTER_IN_PARENT);
          gameParams.bottomMargin = 1;
    
       adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
                    adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                    adParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    
        layout.addView(gameView, gameParams);
        layout.addView(adView, adParams);
    
        if (mHelper == null) {
            getGameHelper();
        }
    
        mHelper.setup(this);
    
        setContentView(layout);
    
    }
    

    开始快速游戏:

        private void startQuickGame(){
        Log.d(TAG, "StartQuickGame()");
    
        final int MIN_OPPONENTS = 1, MAX_OPPONENTS = 1;
    
        Bundle autoMatch = RoomConfig.createAutoMatchCriteria(MIN_OPPONENTS,
                MAX_OPPONENTS, 0);
         RoomConfig.Builder roomBuilder = RoomConfig.builder(this);
         roomBuilder.setMessageReceivedListener(this);
         roomBuilder.setRoomStatusUpdateListener(this);
         roomBuilder.setAutoMatchCriteria(autoMatch);
    
    
         Games.RealTimeMultiplayer.create(getApiClient(), roomBuilder.build());
    
        //prevent screen from sleeping during players matching
    
         Log.d(TAG, " startQuickGame()" +  "createRoom called");
    
       }
    

    创建房间

        @Override
    public void onRoomCreated(int statusCode, Room room) {
    
        Log.d(TAG, "On room created()");
        if(statusCode != GamesClient.STATUS_OK){
            Log.d(TAG, "On room created() ERROR");
            return;
        }
        showWaitingRoom(room);
    
      }
    

    连接到房间

       @Override
    public void onConnectedToRoom(Room room) {
         Log.d(TAG, "onConnectedToRoom.");
    
            // get room ID, participants and my ID:
            mRoomId = room.getRoomId();
            mParticipants = room.getParticipants();
            mMyId = room.getParticipantId(Games.Players.getCurrentPlayerId(getApiClient()));
    
    
            // print out the list of participants (for debug purposes)
            Log.d(TAG, "Room ID: " + mRoomId);
            Log.d(TAG, "My ID " + mMyId);
            Log.d(TAG, "<< CONNECTED TO ROOM>>");
    
    }
    

    SHOW WAITING ROOM

    void showWaitingRoom(Room room) {
        // minimum number of players required for our game
        // For simplicity, we require everyone to join the game before we start it
        // (this is signaled by Integer.MAX_VALUE).
        final int MIN_PLAYERS = Integer.MAX_VALUE;
        Intent i = Games.RealTimeMultiplayer.getWaitingRoomIntent(getApiClient(), room, MIN_PLAYERS);
    
        // show waiting room UI
        startActivityForResult(i, RC_WAITING_ROOM);
    }
    

    切换到游戏画面

      public void startGame(boolean multiPlayer){
        Log.d(TAG, "startGame()");
        mMultiplayer = multiPlayer;
    
        gameActivity.setScreen(new MultiTest(gameActivity)); //Game Screen Libgdx
    
        Log.d(TAG, "Switched to MultiTest game screen");
    
    }
    

1 个答案:

答案 0 :(得分:0)

最终呼叫所有玩家说房间已准备好开始播放,是OnRoomConnected。

使用该电话启动游戏屏幕

来自Google Developer Site

  

onRoomConnected(int statusCode,Room room)   当实时房间中的所有参与者完全连接时调用。

如果您使用其他任何内容,可能会产生意想不到的后果。

另外,不要忘记以编程方式关闭候诊室

@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");
    }

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

}