2016-03-03 114 views
0

我一直在尝试了谷歌播放服务多人APK,并且已经尝试了示例代码“按钮唱首歌”,如下图所示。Play整合服务与LibGDX

但是,我不知道如何移植这种过度到LibGDX作为目前代码运行基于断没有“核心”和“androidlauncher”那libGDX有一个正常的项目。有人可能有任何建议我应该怎么做?据我所知,核心有一个名为render()的方法,它总是循环运行。但是我不能想到一种整合它的方法。

我检查了几个websites,但是,我可以问为什么我不能将整个Buttonclicker项目放入核心模块并调用它的方法?喜欢登录和注销。

我只需要的功能目前在按钮Clicker的实施

代码如下:

/* Copyright (C) 2013 Google Inc. 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 

package com.ryanhello; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.WindowManager; 
import android.widget.TextView; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.games.Games; 
import com.google.android.gms.games.GamesStatusCodes; 
import com.google.android.gms.games.GamesActivityResultCodes; 
import com.google.android.gms.games.multiplayer.Invitation; 
import com.google.android.gms.games.multiplayer.Multiplayer; 
import com.google.android.gms.games.multiplayer.OnInvitationReceivedListener; 
import com.google.android.gms.games.multiplayer.Participant; 
import com.google.android.gms.games.multiplayer.realtime.RealTimeMessage; 
import com.google.android.gms.games.multiplayer.realtime.RealTimeMessageReceivedListener; 
import com.google.android.gms.games.multiplayer.realtime.Room; 
import com.google.android.gms.games.multiplayer.realtime.RoomConfig; 
import com.google.android.gms.games.multiplayer.realtime.RoomStatusUpdateListener; 
import com.google.android.gms.games.multiplayer.realtime.RoomUpdateListener; 
import com.google.android.gms.plus.Plus; 

import com.google.example.games.basegameutils.BaseGameUtils; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Map; 
import java.util.Set; 

/** 
* Button Clicker 2000. A minimalistic game showing the multiplayer features of 
* the Google Play game services API. The objective of this game is clicking a 
* button. Whoever clicks the button the most times within a 20 second interval 
* wins. It's that simple. This game can be played with 2, 3 or 4 players. The 
* code is organized in sections in order to make understanding as clear as 
* possible. We start with the integration section where we show how the game 
* is integrated with the Google Play game services API, then move on to 
* game-specific UI and logic. 
* 
* INSTRUCTIONS: To run this sample, please set up 
* a project in the Developer Console. Then, place your app ID on 
* res/values/ids.xml. Also, change the package name to the package name you 
* used to create the client ID in Developer Console. Make sure you sign the 
* APK with the certificate whose fingerprint you entered in Developer  Console 
* when creating your Client Id. 
* 
* @author Bruno Oliveira (btco), 2013-04-26 
*/ 
public class MainActivity extends Activity 
    implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, 
View.OnClickListener, RealTimeMessageReceivedListener, 
RoomStatusUpdateListener, RoomUpdateListener, OnInvitationReceivedListener { 

/* 
* API INTEGRATION SECTION. This section contains the code that integrates 
* the game with the Google Play game services API. 
*/ 

final static String TAG = "ButtonClicker2000"; 

// Request codes for the UIs that we show with startActivityForResult: 
final static int RC_SELECT_PLAYERS = 10000; 
final static int RC_INVITATION_INBOX = 10001; 
final static int RC_WAITING_ROOM = 10002; 

    // Request code used to invoke sign in user interactions. 
    private static final int RC_SIGN_IN = 9001; 

    // Client used to interact with Google APIs. 
    private GoogleApiClient mGoogleApiClient; 

    // Are we currently resolving a connection failure? 
    private boolean mResolvingConnectionFailure = false; 

    // Has the user clicked the sign-in button? 
    private boolean mSignInClicked = false; 

    // Set to true to automatically start the sign in flow when the Activity starts. 
    // Set to false to require the user to click the button in order to sign in. 
    private boolean mAutoStartSignInFlow = true; 

    // Room ID where the currently active game is taking place; null if we're 
    // not playing. 
    String mRoomId = null; 

// Are we playing in multiplayer mode? 
    boolean mMultiplayer = false; 

// The participants in the currently active game 
    ArrayList<Participant> mParticipants = null; 

// My participant ID in the currently active game 
    String mMyId = null; 

// If non-null, this is the id of the invitation we received via the 
// invitation listener 
    String mIncomingInvitationId = null; 

// Message buffer for sending messages 
    byte[] mMsgBuf = new byte[2]; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

// Create the Google Api Client with access to Plus and Games 
mGoogleApiClient = new GoogleApiClient.Builder(this) 
    .addConnectionCallbacks(this) 
    .addOnConnectionFailedListener(this) 
    .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN) 
    .addApi(Games.API).addScope(Games.SCOPE_GAMES) 
    .build(); 

// set up a click listener for everything we care about 
for (int id : CLICKABLES) { 
    findViewById(id).setOnClickListener(this); 
} 
    } 

    @Override 
    public void onClick(View v) { 
Intent intent; 

    switch (v.getId()) { 
     case R.id.button_single_player: 
     case R.id.button_single_player_2: 
      // play a single-player game 
      resetGameVars(); 
      startGame(false); 
      break; 
     case R.id.button_sign_in: 
      // user wants to sign in 
      // Check to see the developer who's running this sample code read the instructions :-) 
      // NOTE: this check is here only because this is a sample! Don't include this 
      // check in your actual production app. 
      if (!BaseGameUtils.verifySampleSetup(this, R.string.app_id)) { 
       Log.w(TAG, "*** Warning: setup problems detected. Sign in may not work!"); 
      } 

      // start the sign-in flow 
      Log.d(TAG, "Sign-in button clicked"); 
      mSignInClicked = true; 
      mGoogleApiClient.connect(); 
     break; 
     case R.id.button_sign_out: 
      // user wants to sign out 
      // sign out. 
      Log.d(TAG, "Sign-out button clicked"); 
      mSignInClicked = false; 
      Games.signOut(mGoogleApiClient); 
      mGoogleApiClient.disconnect(); 
      switchToScreen(R.id.screen_sign_in); 
      break; 
     case R.id.button_invite_players: 
      // show list of invitable players 
      intent = Games.RealTimeMultiplayer.getSelectOpponentsIntent(mGoogleApiClient, 1, 3); 
      switchToScreen(R.id.screen_wait); 
      startActivityForResult(intent, RC_SELECT_PLAYERS); 
      break; 
     case R.id.button_see_invitations: 
      // show list of pending invitations 
      intent = Games.Invitations.getInvitationInboxIntent(mGoogleApiClient); 
      switchToScreen(R.id.screen_wait); 
      startActivityForResult(intent, RC_INVITATION_INBOX); 
      break; 
     case R.id.button_accept_popup_invitation: 
      // user wants to accept the invitation shown on the invitation popup 
      // (the one we got through the OnInvitationReceivedListener). 
      acceptInviteToRoom(mIncomingInvitationId); 
      mIncomingInvitationId = null; 
      break; 
     case R.id.button_quick_game: 
      // user wants to play against a random opponent right now 
      startQuickGame(); 
      break; 
     case R.id.button_click_me: 
      // (gameplay) user clicked the "click me" button 
      scoreOnePoint(); 
      break; 
    } 
} 

void startQuickGame() { 
    // quick-start a game with 1 randomly selected opponent 
    final int MIN_OPPONENTS = 1, MAX_OPPONENTS = 1; 
    Bundle autoMatchCriteria = RoomConfig.createAutoMatchCriteria(MIN_OPPONENTS, 
      MAX_OPPONENTS, 0); 
    RoomConfig.Builder rtmConfigBuilder = RoomConfig.builder(this); 
    rtmConfigBuilder.setMessageReceivedListener(this); 
    rtmConfigBuilder.setRoomStatusUpdateListener(this); 
    rtmConfigBuilder.setAutoMatchCriteria(autoMatchCriteria); 
    switchToScreen(R.id.screen_wait); 
    keepScreenOn(); 
    resetGameVars(); 
    Games.RealTimeMultiplayer.create(mGoogleApiClient, rtmConfigBuilder.build()); 
} 

@Override 
public void onActivityResult(int requestCode, int responseCode, 
     Intent intent) { 
    super.onActivityResult(requestCode, responseCode, intent); 

    switch (requestCode) { 
     case RC_SELECT_PLAYERS: 
      // we got the result from the "select players" UI -- ready to create the room 
      handleSelectPlayersResult(responseCode, intent); 
      break; 
     case RC_INVITATION_INBOX: 
      // we got the result from the "select invitation" UI (invitation inbox). We're 
      // ready to accept the selected invitation: 
      handleInvitationInboxResult(responseCode, intent); 
      break; 
     case RC_WAITING_ROOM: 
      // we got the result from the "waiting room" UI. 
      if (responseCode == Activity.RESULT_OK) { 
       // ready to start playing 
       Log.d(TAG, "Starting game (waiting room returned OK)."); 
       startGame(true); 
      } else if (responseCode == GamesActivityResultCodes.RESULT_LEFT_ROOM) { 
       // player indicated that they want to leave the room 
       leaveRoom(); 
      } else if (responseCode == Activity.RESULT_CANCELED) { 
       // Dialog was cancelled (user pressed back key, for instance). In our game, 
       // this means leaving the room too. In more elaborate games, this could mean 
       // something else (like minimizing the waiting room UI). 
       leaveRoom(); 
      } 
      break; 
     case RC_SIGN_IN: 
      Log.d(TAG, "onActivityResult with requestCode == RC_SIGN_IN, responseCode=" 
       + responseCode + ", intent=" + intent); 
      mSignInClicked = false; 
      mResolvingConnectionFailure = false; 
      if (responseCode == RESULT_OK) { 
       mGoogleApiClient.connect(); 
      } else { 
       BaseGameUtils.showActivityResultError(this,requestCode,responseCode, R.string.signin_other_error); 
      } 
      break; 
    } 
    super.onActivityResult(requestCode, responseCode, intent); 
} 

// Handle the result of the "Select players UI" we launched when the user clicked the 
// "Invite friends" button. We react by creating a room with those players. 
private void handleSelectPlayersResult(int response, Intent data) { 
    if (response != Activity.RESULT_OK) { 
     Log.w(TAG, "*** select players UI cancelled, " + response); 
     switchToMainScreen(); 
     return; 
    } 

    Log.d(TAG, "Select players UI succeeded."); 

    // get the invitee list 
    final ArrayList<String> invitees = data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS); 
    Log.d(TAG, "Invitee count: " + invitees.size()); 

    // get the automatch criteria 
    Bundle autoMatchCriteria = null; 
    int minAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0); 
    int maxAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0); 
    if (minAutoMatchPlayers > 0 || maxAutoMatchPlayers > 0) { 
     autoMatchCriteria = RoomConfig.createAutoMatchCriteria(
       minAutoMatchPlayers, maxAutoMatchPlayers, 0); 
     Log.d(TAG, "Automatch criteria: " + autoMatchCriteria); 
    } 

    // create the room 
    Log.d(TAG, "Creating room..."); 
    RoomConfig.Builder rtmConfigBuilder = RoomConfig.builder(this); 
    rtmConfigBuilder.addPlayersToInvite(invitees); 
    rtmConfigBuilder.setMessageReceivedListener(this); 
    rtmConfigBuilder.setRoomStatusUpdateListener(this); 
    if (autoMatchCriteria != null) { 
     rtmConfigBuilder.setAutoMatchCriteria(autoMatchCriteria); 
    } 
    switchToScreen(R.id.screen_wait); 
    keepScreenOn(); 
    resetGameVars(); 
    Games.RealTimeMultiplayer.create(mGoogleApiClient, rtmConfigBuilder.build()); 
    Log.d(TAG, "Room created, waiting for it to be ready..."); 
} 

// Handle the result of the invitation inbox UI, where the player can pick an invitation 
// to accept. We react by accepting the selected invitation, if any. 
private void handleInvitationInboxResult(int response, Intent data) { 
    if (response != Activity.RESULT_OK) { 
     Log.w(TAG, "*** invitation inbox UI cancelled, " + response); 
     switchToMainScreen(); 
     return; 
    } 

    Log.d(TAG, "Invitation inbox UI succeeded."); 
    Invitation inv = data.getExtras().getParcelable(Multiplayer.EXTRA_INVITATION); 

    // accept invitation 
    acceptInviteToRoom(inv.getInvitationId()); 
} 

// Accept the given invitation. 
void acceptInviteToRoom(String invId) { 
    // accept the invitation 
    Log.d(TAG, "Accepting invitation: " + invId); 
    RoomConfig.Builder roomConfigBuilder = RoomConfig.builder(this); 
    roomConfigBuilder.setInvitationIdToAccept(invId) 
      .setMessageReceivedListener(this) 
      .setRoomStatusUpdateListener(this); 
    switchToScreen(R.id.screen_wait); 
    keepScreenOn(); 
    resetGameVars(); 
    Games.RealTimeMultiplayer.join(mGoogleApiClient, roomConfigBuilder.build()); 
} 

// Activity is going to the background. We have to leave the current room. 
@Override 
public void onStop() { 
    Log.d(TAG, "**** got onStop"); 

    // if we're in a room, leave it. 
    leaveRoom(); 

    // stop trying to keep the screen on 
    stopKeepingScreenOn(); 

    if (mGoogleApiClient == null || !mGoogleApiClient.isConnected()){ 
     switchToScreen(R.id.screen_sign_in); 
    } 
    else { 
     switchToScreen(R.id.screen_wait); 
    } 
    super.onStop(); 
    } 

// Activity just got to the foreground. We switch to the wait screen because we will now 
// go through the sign-in flow (remember that, yes, every time the Activity comes back to the 
// foreground we go through the sign-in flow -- but if the user is already authenticated, 
// this flow simply succeeds and is imperceptible). 
@Override 
public void onStart() { 
    switchToScreen(R.id.screen_wait); 
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { 
     Log.w(TAG, 
      "GameHelper: client was already connected on onStart()"); 
    } else { 
     Log.d(TAG,"Connecting client."); 
     mGoogleApiClient.connect(); 
    } 
    super.onStart(); 
} 

// Handle back key to make sure we cleanly leave a game if we are in the middle of one 
@Override 
public boolean onKeyDown(int keyCode, KeyEvent e) { 
    if (keyCode == KeyEvent.KEYCODE_BACK && mCurScreen == R.id.screen_game) { 
     leaveRoom(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, e); 
} 

// Leave the room. 
void leaveRoom() { 
    Log.d(TAG, "Leaving room."); 
    mSecondsLeft = 0; 
    stopKeepingScreenOn(); 
    if (mRoomId != null) { 
     Games.RealTimeMultiplayer.leave(mGoogleApiClient, this, mRoomId); 
     mRoomId = null; 
     switchToScreen(R.id.screen_wait); 
    } else { 
     switchToMainScreen(); 
    } 
} 

// Show the waiting room UI to track the progress of other players as they enter the 
// room and get connected. 
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(mGoogleApiClient, room, MIN_PLAYERS); 

    // show waiting room UI 
    startActivityForResult(i, RC_WAITING_ROOM); 
} 

// Called when we get an invitation to play a game. We react by showing that to the user. 
@Override 
public void onInvitationReceived(Invitation invitation) { 
    // We got an invitation to play a game! So, store it in 
    // mIncomingInvitationId 
    // and show the popup on the screen. 
    mIncomingInvitationId = invitation.getInvitationId(); 
    ((TextView) findViewById(R.id.incoming_invitation_text)).setText(
      invitation.getInviter().getDisplayName() + " " + 
        getString(R.string.is_inviting_you)); 
    switchToScreen(mCurScreen); // This will show the invitation popup 
} 

@Override 
public void onInvitationRemoved(String invitationId) { 

    if (mIncomingInvitationId.equals(invitationId)&&mIncomingInvitationId!=null) { 
     mIncomingInvitationId = null; 
     switchToScreen(mCurScreen); // This will hide the invitation popup 
    } 

} 

/* 
* CALLBACKS SECTION. This section shows how we implement the several games 
* API callbacks. 
*/ 

@Override 
public void onConnected(Bundle connectionHint) { 
    Log.d(TAG, "onConnected() called. Sign in successful!"); 

    Log.d(TAG, "Sign-in succeeded."); 

    // register listener so we are notified if we receive an invitation to play 
    // while we are in the game 
    Games.Invitations.registerInvitationListener(mGoogleApiClient, this); 

    if (connectionHint != null) { 
    Log.d(TAG, "onConnected: connection hint provided. Checking for invite."); 
    Invitation inv = connectionHint 
     .getParcelable(Multiplayer.EXTRA_INVITATION); 
    if (inv != null && inv.getInvitationId() != null) { 
     // retrieve and cache the invitation ID 
     Log.d(TAG,"onConnected: connection hint has a room invite!"); 
     acceptInviteToRoom(inv.getInvitationId()); 
     return; 
    } 
    } 
    switchToMainScreen(); 

} 

@Override 
public void onConnectionSuspended(int i) { 
    Log.d(TAG, "onConnectionSuspended() called. Trying to reconnect."); 
    mGoogleApiClient.connect(); 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    Log.d(TAG, "onConnectionFailed() called, result: " + connectionResult); 

    if (mResolvingConnectionFailure) { 
    Log.d(TAG, "onConnectionFailed() ignoring connection failure; already resolving."); 
    return; 
    } 

    if (mSignInClicked || mAutoStartSignInFlow) { 
    mAutoStartSignInFlow = false; 
    mSignInClicked = false; 
    mResolvingConnectionFailure = BaseGameUtils.resolveConnectionFailure(this, mGoogleApiClient, 
     connectionResult, RC_SIGN_IN, getString(R.string.signin_other_error)); 
    } 

    switchToScreen(R.id.screen_sign_in); 
} 

// Called when we are connected to the room. We're not ready to play yet! (maybe not everybody 
// is connected yet). 
@Override 
public void onConnectedToRoom(Room room) { 
    Log.d(TAG, "onConnectedToRoom."); 

    //get participants and my ID: 
    mParticipants = room.getParticipants(); 
    mMyId = room.getParticipantId(Games.Players.getCurrentPlayerId(mGoogleApiClient)); 

    // save room ID if its not initialized in onRoomCreated() so we can leave cleanly before the game starts. 
    if(mRoomId==null) 
     mRoomId = room.getRoomId(); 

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

// Called when we've successfully left the room (this happens a result of voluntarily leaving 
// via a call to leaveRoom(). If we get disconnected, we get onDisconnectedFromRoom()). 
@Override 
public void onLeftRoom(int statusCode, String roomId) { 
    // we have left the room; return to main screen. 
    Log.d(TAG, "onLeftRoom, code " + statusCode); 
    switchToMainScreen(); 
} 

// Called when we get disconnected from the room. We return to the main screen. 
@Override 
public void onDisconnectedFromRoom(Room room) { 
    mRoomId = null; 
    showGameError(); 
} 

// Show error message about game being cancelled and return to main screen. 
void showGameError() { 
    BaseGameUtils.makeSimpleDialog(this, getString(R.string.game_problem)); 
    switchToMainScreen(); 
} 

// Called when room has been created 
@Override 
public void onRoomCreated(int statusCode, Room room) { 
    Log.d(TAG, "onRoomCreated(" + statusCode + ", " + room + ")"); 
    if (statusCode != GamesStatusCodes.STATUS_OK) { 
     Log.e(TAG, "*** Error: onRoomCreated, status " + statusCode); 
     showGameError(); 
     return; 
    } 

    // save room ID so we can leave cleanly before the game starts. 
    mRoomId = room.getRoomId(); 

    // show the waiting room UI 
    showWaitingRoom(room); 
} 

// Called when room is fully connected. 
@Override 
public void onRoomConnected(int statusCode, Room room) { 
    Log.d(TAG, "onRoomConnected(" + statusCode + ", " + room + ")"); 
    if (statusCode != GamesStatusCodes.STATUS_OK) { 
     Log.e(TAG, "*** Error: onRoomConnected, status " + statusCode); 
     showGameError(); 
     return; 
    } 
    updateRoom(room); 
} 

@Override 
public void onJoinedRoom(int statusCode, Room room) { 
    Log.d(TAG, "onJoinedRoom(" + statusCode + ", " + room + ")"); 
    if (statusCode != GamesStatusCodes.STATUS_OK) { 
     Log.e(TAG, "*** Error: onRoomConnected, status " + statusCode); 
     showGameError(); 
     return; 
    } 

    // show the waiting room UI 
    showWaitingRoom(room); 
} 

// We treat most of the room update callbacks in the same way: we update our list of 
// participants and update the display. In a real game we would also have to check if that 
// change requires some action like removing the corresponding player avatar from the screen, 
// etc. 
@Override 
public void onPeerDeclined(Room room, List<String> arg1) { 
    updateRoom(room); 
} 

@Override 
public void onPeerInvitedToRoom(Room room, List<String> arg1) { 
    updateRoom(room); 
} 

@Override 
public void onP2PDisconnected(String participant) { 
} 

@Override 
public void onP2PConnected(String participant) { 
} 

@Override 
public void onPeerJoined(Room room, List<String> arg1) { 
    updateRoom(room); 
} 

@Override 
public void onPeerLeft(Room room, List<String> peersWhoLeft) { 
    updateRoom(room); 
} 

@Override 
public void onRoomAutoMatching(Room room) { 
    updateRoom(room); 
} 

@Override 
public void onRoomConnecting(Room room) { 
    updateRoom(room); 
} 

@Override 
public void onPeersConnected(Room room, List<String> peers) { 
    updateRoom(room); 
} 

@Override 
public void onPeersDisconnected(Room room, List<String> peers) { 
    updateRoom(room); 
} 

void updateRoom(Room room) { 
    if (room != null) { 
     mParticipants = room.getParticipants(); 
    } 
    if (mParticipants != null) { 
     updatePeerScoresDisplay(); 
    } 
} 

My current interface code 包com.macrohard.game;

import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.backends.android.AndroidApplication; 
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration; 
import com.google.example.games.basegameutils.GameHelper; 
import com.macrohard.game.SomeGame; 

import com.google.android.gms.games.Games; 
import com.google.example.games.basegameutils.GameHelper; 
import com.google.example.games.basegameutils.GameHelper.GameHelperListener; 

import com.macrohard.game.ActionResolver; 
import com.macrohard.game.MainMenu; 

public class AndroidLauncher extends AndroidApplication implements ActionResolver { 
private GameHelper gameHelper; 
private final static int requestCode = 1; 
@Override 
protected void onCreate (Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); 

    gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES); 
    gameHelper.enableDebugLog(false); 

    GameHelper.GameHelperListener gameHelperListener = new GameHelper.GameHelperListener() 
    { 
     @Override 
     public void onSignInFailed(){ } 

     @Override 
     public void onSignInSucceeded(){ } 
    }; 

    gameHelper.setup(gameHelperListener); 
    //initialize(new SomeGame(), config); 
    initialize(new MainMenu(this), config); 
} 

//... 

@Override 
protected void onStart() 
{ 
    super.onStart(); 
    gameHelper.onStart(this); 
} 

@Override 
protected void onStop() 
{ 
    super.onStop(); 
    gameHelper.onStop(); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    gameHelper.onActivityResult(requestCode, resultCode, data); 
} 

@Override 
public void signIn() 
{ 
    try 
    { 
     runOnUiThread(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       gameHelper.beginUserInitiatedSignIn(); 
      } 
     }); 
    } 
    catch (Exception e) 
    { 
     Gdx.app.log("MainActivity", "Log in failed: " + e.getMessage() + "."); 
    } 
} 

@Override 
public void signOut() 
{ 
    try 
    { 
     runOnUiThread(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       gameHelper.signOut(); 
      } 
     }); 
    } 
    catch (Exception e) 
    { 
     Gdx.app.log("MainActivity", "Log out failed: " + e.getMessage() + "."); 
    } 
} 

@Override 
public void rateGame() 
{ 
    String str = "Your PlayStore Link"; 
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(str))); 
} 

@Override 
public void unlockAchievement() 
{ 

} 

@Override 
public void submitScore(int highScore) 
{ 

} 

@Override 
public void showAchievement() 
{ 

} 

@Override 
public void showScore() 
{ 

} 

@Override 
public boolean isSignedIn() 
{ 
    return gameHelper.isSignedIn(); 
} 

}

回答

1

LibGDX分离平台专有代码成不同的模块,用于通过使用相同的代码基础(核心项目)提供多平台。所以你应该在代码示例中显示的android项目中实现android特定的东西。

您不能在核心项目的“com.google.android”包中使用任何类。因为Google Play服务依赖于Android特定的资源。你应该阅读文档有关平台特定的代码

https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code

所以,你应该准备定义要使用,并为每个人你打算在你的项目,以支持平台单独实现你的方法的接口。

+0

您好!感谢您的意见!我已阅读文档并实现了定义我的方法的接口。目前我只能登录谷歌播放工作,但之后没有其他事情发生,因为我不知道在哪里放置下几种方法,例如我想创建一个多人空间使用Roomconfig类似的按钮代码张贴在上面。你有什么建议在哪里放置它?我也可以问为什么自动启动登录?我只初始化动作解析器的主要活动,但没有调用任何方法,如signIn() –

+0

你碰巧有一个LibGDX实现多人游戏服务的例子吗?无论如何,我可以与你保持联系吗?非常感谢!!!! –

0

看来,我们都试图使用谷歌Play游戏服务任何libGDX项目的非常具体的架构,以实现实时多人。我建议你看看这GitHub存储库:GarrapuchoFootball。告诉我,如果它可以帮助你。

+0

好吧我会看看它!谢谢!无论如何,我可以和你联系吗? –