简体   繁体   English

Android上的Google实时多人游戏

[英]Google Real-time Multiplayer on Android

I am having trouble with my app that uses Google's Real-time Multiplayer. 我的应用程序使用Google的实时多人游戏时遇到问题。 When you launch the app, it just crashes. 启动应用程序时,它只会崩溃。 I added the crash log at the bottom... 我在底部添加了崩溃日志...

I was able to find out that the problem was when I called mGoogleApiClient.connect(); 我发现问题出在我调用mGoogleApiClient.connect();

Here is my code: 这是我的代码:

//(Your package here:)
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 android.widget.Toast;

import com.birdprograms.multiplayer.BaseGameUtils.src.main.java.com.google.example.games.basegameutils.BaseGameUtils;
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.multiplayer.Invitation;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MyActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, View.OnClickListener, RealTimeMessageReceivedListener, RoomUpdateListener, RoomStatusUpdateListener, OnInvitationReceivedListener{
    private static final int RC_SIGN_IN = 9001;
    private GoogleApiClient mGoogleApiClient;
    private boolean mResolvingConnectionFailure = false;
    private boolean mSignInClicked = false;
    private boolean mAutoStartSignInFlow = true;
    String mRoomId = null;
    ArrayList<Participant> mParrticipants = null;
    String mMyId = null;
    byte[] mMsgBuf = new byte[2];
    private TextView textView;
    private TextView myscoretxt;
    private TextView urscoretxt;
    final static int[] CLICKALBES = {
            R.id.button,R.id.button2,R.id.button3
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        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();
        for (int id : CLICKALBES){
            findViewById(id).setOnClickListener(this);
        }
    }
    private void startQuickGame(){
        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);
        changedisplay(1);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        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_SIGN_IN:
                mSignInClicked = false;
                mResolvingConnectionFailure = false;
                if(responseCode == RESULT_OK){
                    mGoogleApiClient.connect();
                } else {
                    BaseGameUtils.showActivityResultError(this, requestCode, requestCode, R.string.signin_other_error);
                }
                break;

        };
        super.onActivityResult(requestCode,responseCode,intent);
    }
    @Override
    public void onStop(){
        leaveRoom();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        if (mGoogleApiClient == null || !mGoogleApiClient.isConnected()){
            changedisplay(0);
        }
        super.onStop();
    }
    @Override
    public void onStart(){
        changedisplay(0);
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()){

        } else {
            mGoogleApiClient.connect();
        }
        super.onStart();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent e){
        if (keyCode == KeyEvent.KEYCODE_BACK && findViewById(R.id.button).getVisibility() == View.VISIBLE){
            leaveRoom();
            return true;
        }
        return super.onKeyDown(keyCode, e);
    }
    void leaveRoom(){
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        resetGameVars();
        if(mRoomId != null){
            Games.RealTimeMultiplayer.leave(mGoogleApiClient, this, mRoomId);
            mRoomId = null;
            findViewById(R.id.button).setVisibility(View.VISIBLE);
            changedisplay(0);
            textView = (TextView)findViewById(R.id.textView3);
            myscoretxt = (TextView)findViewById(R.id.textView);
            urscoretxt = (TextView)findViewById(R.id.textView2);
        }
    }    
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                mSignInClicked = true;
                mGoogleApiClient.connect();
            break;
            case R.id.button2:
                startQuickGame();
                startGame(true);
            break;
            case R.id.button3:
                scoreOnePoint();
        }

    }
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (mResolvingConnectionFailure){
            return;
        }
        if (mSignInClicked || mAutoStartSignInFlow){
            mAutoStartSignInFlow = false;
            mSignInClicked = false;
            mResolvingConnectionFailure = BaseGameUtils.resolveConnectionFailure(this,mGoogleApiClient,connectionResult,RC_SIGN_IN,getString(R.string.signin_other_error));
        }
        changedisplay(1);
    }
    void updateRoom(Room room){
        if(room != null){
            mParrticipants = room.getParticipants();
        }
        if (mParrticipants != null){
            updatePeerScoresDisplay();
        }
    }    
    @Override
    public void onConnectedToRoom(Room room) {
        mRoomId = room.getRoomId();
        mParrticipants = room.getParticipants();
        mMyId = room.getParticipantId(Games.Players.getCurrentPlayerId(mGoogleApiClient));
    }

    @Override
    public void onDisconnectedFromRoom(Room room) {
        mRoomId = null;
        showGameError();
    }
    void showGameError(){
        BaseGameUtils.makeSimpleDialog(this,getString(R.string.game_problem));
        changedisplay(0);
    }
    @Override
    public void onJoinedRoom(int i, Room room) {
        if(i != GamesStatusCodes.STATUS_OK){
            showGameError();
            return;
        }
    }
    @Override
    public void onRoomConnected(int i, Room room) {
        if(i != GamesStatusCodes.STATUS_OK){
            showGameError();
            return;
        }
        updateRoom(room);
    }
    int mSecondsLeft = -1;
    final static  int GAME_DDURATION = 20;
    int mScore = 0;
    void resetGameVars(){
        mSecondsLeft = GAME_DDURATION;
        mScore = 0;
        mParticipantScore.clear();
        mFinishedParticipants.clear();
    }
    void startGame(boolean multiplayer){
        updateScoreDisplay();
        broadcastScore(false);
        changedisplay(1);
        final Handler h = new Handler();
        h.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mSecondsLeft <= 0)
                    return;
                gameTick();
                h.postDelayed(this, 1000);
            }
        }, 1000);
    }
    void gameTick(){
        if(mSecondsLeft > 0)
            --mSecondsLeft;
        textView.setText("Time Left: "+mSecondsLeft+" seconds");
        if (mSecondsLeft <= 0){
            changedisplay(0);
            broadcastScore(true);
        }
    }
    void scoreOnePoint(){
        if(mSecondsLeft <= 0)
            return;
        ++mScore;
        updateScoreDisplay();
        updatePeerScoresDisplay();
        broadcastScore(false);
    }
    Map<String, Integer> mParticipantScore = new HashMap<String, Integer>();
    Set<String> mFinishedParticipants = new HashSet<String>();

    @Override
    public void onRealTimeMessageReceived(RealTimeMessage realTimeMessage) {
        byte[] buf = realTimeMessage.getMessageData();
        String sender = realTimeMessage.getSenderParticipantId();

        if(buf[0] == 'F' || buf[0] == 'U'){
            int existingScore = mParticipantScore.containsKey(sender) ?
                    mParticipantScore.get(sender) : 0;
            int thisScore = (int) buf[1];
            if(thisScore > existingScore){
                mParticipantScore.put(sender, thisScore);

            }
            updatePeerScoresDisplay();
            if((char) buf[0] == 'F'){
                mFinishedParticipants.add(realTimeMessage.getSenderParticipantId());
            }
        }
    }
    void broadcastScore(boolean finalScore){
        mMsgBuf[0] = (byte) (finalScore ? 'F' : 'U');
        mMsgBuf[1] = (byte) mScore;
        for(Participant p : mParrticipants){
            if(p.getParticipantId().equals(mMyId))
                continue;
            if (p.getStatus() != Participant.STATUS_JOINED)
                continue;
            if (finalScore){
                Games.RealTimeMultiplayer.sendReliableMessage(mGoogleApiClient, null, mMsgBuf, mRoomId, p.getParticipantId());
            } else {
                Games.RealTimeMultiplayer.sendUnreliableMessage(mGoogleApiClient, mMsgBuf, mRoomId, p.getParticipantId());
            }
        }
    }
    void updateScoreDisplay(){
        myscoretxt.setText("Your Score: "+fomatScore(mScore));
    }
    String fomatScore(int i){
        if(i<0)
           i=0;
        String s = String.valueOf(i);
        return s.length() == 1 ? "00" + s : s.length() == 2 ? "0" + s : s;
    }
    void updatePeerScoresDisplay(){
        if(mRoomId != null){
            for (Participant p : mParrticipants){
                String pid = p.getParticipantId();
                if(pid.equals(mMyId))
                    continue;
                if (p.getStatus() != Participant.STATUS_JOINED)
                    continue;
                int score = mParticipantScore.containsKey(pid) ? mParticipantScore.get(pid) : 0;
                urscoretxt.setText("Opponents Score: "+fomatScore(score));
            }
        }
    }
    void changedisplay(int i){
        if(i == 0){
            findViewById(R.id.button).setVisibility(View.VISIBLE);
            findViewById(R.id.button2).setVisibility(View.VISIBLE);
            findViewById(R.id.button3).setVisibility(View.GONE);
            findViewById(R.id.textView).setVisibility(View.GONE);
            findViewById(R.id.textView2).setVisibility(View.GONE);
            findViewById(R.id.textView3).setVisibility(View.GONE);
        }
        else{
            findViewById(R.id.button).setVisibility(View.GONE);
            findViewById(R.id.button2).setVisibility(View.GONE);
            findViewById(R.id.button3).setVisibility(View.VISIBLE);
            findViewById(R.id.textView).setVisibility(View.VISIBLE);
            findViewById(R.id.textView2).setVisibility(View.VISIBLE);
            findViewById(R.id.textView3).setVisibility(View.VISIBLE);
        }
    }
} 

I have removed several callbacks for readers sake that did not cause the problem for sure! 为了读者的方便,我删除了一些回调,这些回调肯定不会引起问题! If you read the whole thing, then thank you for your patience. 如果您阅读了整本书,则感谢您的耐心配合。 :) :)

Any help, or suggestions would be helpful. 任何帮助或建议都会有所帮助。 Thanks! 谢谢!

-Nathan -Nathan

Edit 编辑

Here is the crash log: 这是崩溃日志:

04-22 09:28:00.320  19247-19247/com.birdprograms.multiplayerdemo E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.birdprograms.multiplayerdemo, PID: 19247
    java.lang.IllegalStateException: A fatal developer error has occurred. Check the logs for further information.
            at com.google.android.gms.internal.jl$h.b(Unknown Source)
            at com.google.android.gms.internal.jl$h.g(Unknown Source)
            at com.google.android.gms.internal.jl$b.hy(Unknown Source)
            at com.google.android.gms.internal.jl$a.handleMessage(Unknown Source)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5323)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)

Your manifest is missing 您的清单不见了

<meta-data
      android:name="com.google.android.gms.games.APP_ID"
      android:value="@string/app_id" />
// this is required if your RequestedClients is using AppStateManager.SCOPE_APP_STATE
<meta-data
      android:name="com.google.android.gms.games.appstate.APP_ID"
      android:value="@string/app_id" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中开发实时多人游戏 - Developing a Real-time Multiplayer Game in Android 谷歌实时多人api示例:ButtonClicker - google real-time multiplayer api example: ButtonClicker Google玩实时多人并发-有时玩家会在同一时间选择正确的答案 - Google play real-time multiplayer concurrency - sometimes players pick the right answer at the same time Google服务中的实时多人游戏的问题(向您的房间配置添加独家位掩码) - The issue with real-time multiplayer from google services (adding an exclusive bitmask to your room configuration) 当应用程序进入后台时,实时多人游戏Google Play游戏服务同行会断开连接 - Real-time Multiplayer Google Play Games Services peer gets disconnected when app goes in background 如何在实时多人Google Play游戏服务中创建房间时向其他参与者发送数据 - How to send data to other participant on Room creation in Real-time Multiplayer Google Play Game Services 如何在使用Google Play游戏服务的实时多人游戏中使用僵尸玩家? - How can I use bot players in real-time multiplayer games using Google Play game services? 如何使用Google Play游戏在实时多人游戏中处理应用版本? - How to handle app versions in real-time multiplayer using Google Play Games? 如何检测客户是否故意在Google Play游戏实时多人游戏中留下房间? - How to detect if client intentionally left Room in Google Play Games Real-time Multiplayer? Google Play实时多人游戏无法在Genymotion虚拟设备上运行 - Google Play real-time multiplayer does not work on a Genymotion virtual device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM