call Activity when click button on core game LibGDX

谁说我不能喝 提交于 2019-12-24 17:56:52

问题


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();

            }
        });

Here is my interface

 public interface MyGameCallback{
            public void startActivity();
        }

        private MyGameCallback myGameCallback;

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

        }

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

}

Please help me, thank you so much.


回答1:


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();
        }

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.



来源:https://stackoverflow.com/questions/30173733/call-activity-when-click-button-on-core-game-libgdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!