呼叫活动时,在核心游戏LibGDX点击按钮 [英] call Activity when click button on core game LibGDX

查看:303
本文介绍了呼叫活动时,在核心游戏LibGDX点击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对核心游戏的按钮。我想,当我点击按钮,活动将运行。我想我可以使用接口,但它不工作。

I have a button on Core game. I want when i click button, a Activity will run. I think i can use Interface but it do not work.

buttonPost.addListener(new ClickListener(){

            @Override
            public void clicked(InputEvent event, float x, float y) {
                myGameCallback.startActivity();

            }
        });

下面是我的界面

 public interface MyGameCallback{
            public void startActivity();
        }

        private MyGameCallback myGameCallback;

        public void setMyGameCallback(MyGameCallback callback){
            myGameCallback=callback;

        }

和Android code:

And android code:

public class MainActivity extends AndroidApplication implements Main.MyGameCallback {

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

        Main main=new Main();
        main.setMyGameCallback(this);

        initialize(main, config);
    }

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

}

请帮帮我,谢谢你了。

推荐答案

接口的方式实现一些原生的Andr​​oid功能在LIBGDX的工作原理如下。结果
首先,为什么你的主类实现屏幕?据我所知道的主类应实现游戏。屏幕不同,那么主类。
所以在这里你去界面。

The Interface way to implement some native android functionality in LIBGDX works as follows.
First of all why your Main class implement screen? As far as I know the Main Class should implement Game. Screen is different then main class. So here you go for Interface.

 public interface MyGameCallback{
            public void startActivity();
        }

在AndroidLauncher

On AndroidLauncher

public class AndroidLauncher extends AndroidApplication implements
    MyGameCallback{

// provide implementation of startActivity(); method. 

}

最后,你的主游戏类应该像

Finally your main Game class should go like

public class MainGame extends Game {


    public MyGameCallback myGameCallback;



    public MainGame(MyGameCallback myGameCallback) {
        super();
        this.myGameCallback = myGameCallback; // initialize in constructor

    }

最后,你想上哪儿开始您的活动做到像在游戏中的按钮。

Finally on the button in game where you want to start your activity do it like.

public class MainMenuScreen implements Screen { 

private MainGame mygame;

    public MainmenuScreen(MainGame game) { // initiaize maingame class so that you can call Interface method by its reference 

this.mygame = game;
}

///////////////////////////////////

///////////////////////////////////

最后,在你的按钮来调用这个方法

Finally on your button do call the method

buttonPost=new TextButton("Post",skin);
            buttonPost.pad(20);
            buttonPost.addListener(new EventListener() {

                @Override
                public boolean handle(Event event) {
                    mygame.myGameCallback.startActivity();
                    return true;
                }
            });

如果您仅通过接口名称它不会工作调用它。

If You call it by just Interface name it wont work.

这篇关于呼叫活动时,在核心游戏LibGDX点击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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