Android的Libgdx,调用在比赛结束后的另一项活动启动按钮上点击 [英] Android-Libgdx, Calling Another Activity after the Game starts on Button click

查看:113
本文介绍了Android的Libgdx,调用在比赛结束后的另一项活动启动按钮上点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的一个主要问题,当我需要调用另一个活动是在比赛开始后,单击按钮时。本场比赛是通过启动(游戏)的Andr​​oidApplication接口方法调用。

I faced a major problem when I need to call another activity when the button is clicked after the Game is started. The Game is called via initiate(game, ) method from AndroidApplication interface.

在正常的活动,我可以方便地调用另一个活动,但它似乎是很难叫从实现AndroidApplication Libgdx类另一个活动。

In normal Activity, I can easily call the another Activity but it seems to be difficult to call another Activity from Libgdx class that implements AndroidApplication.

任何人都可以提出一个适当的方法来调用从实现AndroidApplication接口Libgdx类的活动?

Could anyone suggest a proper method to call the Activity from Libgdx class that implements AndroidApplication interface?

我试图做到这一点的一个星期,但似乎我的方法是完全错误的。

I tried to do this for a week but it seems that my method is totally wrong..

在此先感谢。

推荐答案

定义一个回调接口在你LibGdx类,并用它来通知你的Andr​​oidLauncher开始新的活动。

Define a callback interface in you LibGdx class, and use it to notify your AndroidLauncher to start the new activity.

例如在LibGdx游戏类:

For example in your LibGdx game class:

// Your Game class in the core package
public class MyGame extends Game {

    // Define an interface for your various callbacks to the android launcher
    public interface MyGameCallback {
        public void onStartActivityA();
        public void onStartActivityB();
        public void onStartSomeActivity(int someParameter, String someOtherParameter);
    }

    // Local variable to hold the callback implementation
    private MyGameCallback myGameCallback;

    // ** Additional **
    // Setter for the callback
    public void setMyGameCallback(MyGameCallback callback) {
        myGameCallback = callback;
    }

    @Override
    public void create () {
        ...
    }

    ...

    private void someMethod() {
        ...
        // check the calling class has actually implemented MyGameCallback
        if (myGameCallback != null) {

            // initiate which ever callback method you need.
            if (someCondition) {
                myGameCallback.onStartActivityA();
            } else if (someOtherCondition) {
                myGameCallback.onStartActivityB();
            } else {
                myGameCallback.onStartSomeActivity(someInteger, someString);
            }

        } else {
            Log.e("MyGame", "To use this class you must implement MyGameCallback!")
        }
    }
}

然后确保你的Andr​​oidLauncher实现所需的接口:

Then ensure your AndroidLauncher implements the required interface:

// Your AndroidLauncher
public class AndroidLauncher extends AndroidApplication implements MyGame.MyGameCallback {

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

        // create an instance of MyGame, and set the callback
        MyGame myGame = new MyGame;
        // Since AndroidLauncher implements MyGame.MyGameCallback, we can just pass 'this' to the callback setter.
        myGame.setMyGameCallback(this);

        initialize(myGame, config);
    }

    @Override
    public void onStartActivityA() {
        Intent intent = new Intent(this, ActivityA.class);
        startActivity(intent);
    }

    @Override
    public void onStartActivityB(){
        Intent intent = new Intent(this, ActivityB.class);
        startActivity(intent);
    }

    @Override
    public void onStartSomeActivity(int someParameter, String someOtherParameter){
        Intent intent = new Intent(this, ActivityA.class);

        // do whatever you want with the supplied parameters.
        if (someParameter == 42) {
            intent.putExtra(MY_EXTRA, someOtherParameter);
        }
        startActivity(intent);
    }

}

这篇关于Android的Libgdx,调用在比赛结束后的另一项活动启动按钮上点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆