如何展示&#34;欢迎回来<名称>&#34;使用LibGDX开始游戏的Google Play弹出窗口?</name>

时间:2015-01-26 02:35:11

标签: java android libgdx google-play-services

我在我的项目中使用libGDX工具和BaseGameUtils.GameHelper 每件事看起来都很完美,所有功能都很好。

登录,退出,成就,排行榜一切正常。
除了"Welcome Back <Name>"弹出窗口,当我开始游戏时没有显示。 正如我所说,所有功能都很好。游戏开始时我的游戏会自动开始登录。

但是当我强行退出并再次登录时 在登录流程中,将显示Google Play Services绿色对话框提示用户登录。 点击Sign In后,我的游戏需要一段时间才能连接到服务器并显示Welcome Back弹出窗口。
在本节中,弹出窗口出现在我的游戏中。我很困惑,为什么它有效。

我用我的真实设备测试了很多次 我注意到,如果您之前没有输入Welcome Back diglog,则Google Play Services弹出窗口不会显示。我认为这可能是一些错误的配置或我的错误理解。

我尝试使用setViewForPopups,但它没有用 我想在ViewForPopups连接之前设置GoogleApiClient 但是当我使用这种方式时会出现很多错误。

最后一件事,它不仅发生在Welcome Back弹出窗口中 当我实现某些目标时,它也是一个achievements弹出窗口。

这是我的部分代码:

package com.huskybear.polyknock.android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.badlogic.gdx.Gdx;
import com.google.android.gms.games.Games;
import com.google.android.gms.games.GamesActivityResultCodes;
import com.google.example.games.basegameutils.GameHelper;
import com.huskybear.polyknock.Core.IGoogleServices;

public class AndroidGGServicesHelper implements IGoogleServices {

    private final static int RC_SIGN_IN = 9001;
    private final static int RC_LEADER_BOARDS = 9002;
    private final static int RC_ACHIEVEMENTS = 9003;

    private GameHelper Helper;
    private Activity mActivity;
    private Context mContext;

    private Runnable callback = null;
    private View popups;

    private boolean isSignIn = false;
    private boolean isStartAutoFlow = false;

    public AndroidGGServicesHelper(Activity act, View popups) {

        this.mActivity = act;
        this.mContext = act.getApplicationContext();
        this.popups = popups;

        Helper = new GameHelper(act, GameHelper.CLIENT_GAMES | GameHelper.CLIENT_PLUS);
        GameHelper.GameHelperListener helperListener = new GameHelper.GameHelperListener() {
            @Override
            public void onSignInFailed() {
                if (isStartAutoFlow) {
                    Toast("Unable to sign in.", Toast.LENGTH_SHORT);
                    if (callback != null) {
                        callback.run();
                    }
                }
            }

            @Override
            public void onSignInSucceeded() {
                isSignIn = true;
                if (callback != null) {
                    callback.run();
                }
            }
        };
        Helper.setConnectOnStart(false);
        Helper.setMaxAutoSignInAttempts(0);
        Helper.setup(helperListener);
    }

    @Override
    public void signIn(final boolean isAutoFlow) {
        try {
            mActivity.runOnUiThread(new Runnable() {
                public void run() {
                    if (isAutoFlow) {
                        isStartAutoFlow = true;
                        Helper.reconnectClient();
                    } else {
                        Helper.beginUserInitiatedSignIn();
                    }
                }
            });
        } catch (Exception e) {
            Gdx.app.log("MainActivity", "Log in failed: " + e.getMessage() + ".");
        }
    }

    @Override
    public void signOut() {
        try {
            mActivity.runOnUiThread(new Runnable() {
                //@Override
                public void run() {
                    Helper.signOut();
                    isSignIn = false;
                    Toast("Sign out complete.", Toast.LENGTH_SHORT);
                    if (callback != null) {
                        callback.run();
                    }
                }
            });
        } catch (Exception e) {
            Gdx.app.log("MainActivity", "Log out failed: " + e.getMessage() + ".");
        }
    }

    @Override
    public void achievementUnlock(String type) {
        try {
            Games.Achievements.unlock(Helper.getApiClient(), type);
        } catch (Exception e) {

        }
    }

    @Override
    public void submitScore(int type, long score) {

    }

    @Override
    public void rateGame() {

        Games.setViewForPopups(Helper.getApiClient(), popups);
    }

    @Override
    public void showLeaderboards() {
        try {
            if (isConnected()) {
                mActivity.startActivityForResult(
                        Games.Leaderboards.getAllLeaderboardsIntent(Helper.getApiClient()),
                        RC_LEADER_BOARDS
                );
            } else {
                signIn(false);
            }
        } catch (Exception e) {
            Helper.reconnectClient();
        }
    }

    @Override
    public void showAchievements() {
        try {
            if (isConnected()) {
                mActivity.startActivityForResult(
                        Games.Achievements.getAchievementsIntent(Helper.getApiClient()),
                        RC_ACHIEVEMENTS
                );
            } else {
                signIn(false);
            }
        } catch (Exception e) {
            Helper.reconnectClient();
        }
    }

    @Override
    public void onStart() {
        try {
            Helper.onStart(mActivity);
        } catch (Exception e) {

        }
    }

    @Override
    public void onStop() {
        Helper.onStop();
    }

    @Override
    public void disconnect() {
        Helper.disconnect();
        isSignIn = false;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (((requestCode == RC_LEADER_BOARDS) || (requestCode == RC_ACHIEVEMENTS))
                && resultCode == GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED) {
            Helper.disconnect();
            isSignIn = false;
            if (callback != null) {
                callback.run();
            }
        } else {
            Helper.onActivityResult(requestCode, resultCode, data);
        }
    }

    public boolean isConnected() {
        return Helper.isSignedIn();
    }

    @Override
    public boolean isSignIn() {
        return isSignIn;
    }

    @Override
    public void Toast(final String text, final int length) {
        try {
            mActivity.runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(mContext, text, length).show();
                }
            });
        } catch (Exception e) {

        }
    }

    @Override
    public void setCallback(Runnable run) {
        callback = run;
    }
}     

抱歉我的语言不好。

0 个答案:

没有答案