I am using Libgdx to manage the screens, updates and rendering.
When I get a RESULT_OK from WAITING_ROOM i start the game screen, the following happens.
- in the waiting room, I can see ME who initiated the game room
- I can see the auto matched player who joined the waiting room
- When the MIN players are in the room I get "Prepare to Play"
- The screen is switching to the game screen
Now the issue.
When the new game screen starts one of the players is showing and the other has a blank screen
Sometimes the blank screen will show on the phone of the player who initiated the game and other times on the phone of the player who joined the room.
What am I doing wrong??
See my code:
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);
}
STARTING QUICK GAME:
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");
}
ON CREATE ROOM
@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);
}
ON CONNECTED TO 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);
}
SWITCHING TO GAME SCREEN
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");
}