android - call Activity when click button on core game LibGDX -
i have button on core game. want when click button, activity run. think can use interface not work.
buttonpost.addlistener(new clicklistener(){ @override public void clicked(inputevent event, float x, float y) { mygamecallback.startactivity(); } });
here 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 me, thank much.
the interface way implement native android functionality in libgdx works follows.
first of why main class implement screen? far know main class should implement game. screen different main class. here go interface.
public interface mygamecallback{ public void startactivity(); }
on androidlauncher
public class androidlauncher extends androidapplication implements mygamecallback{ // provide implementation of startactivity(); method. }
finally main game class should go
public class maingame extends game { public mygamecallback mygamecallback; public maingame(mygamecallback mygamecallback) { super(); this.mygamecallback = mygamecallback; // initialize in constructor }
finally on button in game want start activity like.
public class mainmenuscreen implements screen { private maingame mygame; public mainmenuscreen(maingame game) { // initiaize maingame class can call interface method reference this.mygame = game; }
///////////////////////////////////
finally on button call 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 call interface name wont work.
Comments
Post a Comment