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.
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");
}
The Final call to all players to say that the room is ready to begin play, is OnRoomConnected.
Use that call to start up your game Screen
onRoomConnected(int statusCode, Room room) Called when all the participants in a real-time room are fully connected.
If you use anything else, unintended consequences may arise.
also, don't forget to programatically close the waiting 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();
}
}