Grinning Trout
Grinning Trout

Reputation: 119

how can i call a method from another class in libgdx

I want to call the method walo() from render() in classMyGdxGame:

public class MyGdxGame extends ApplicationAdapter{
    public void render() {
        walo();
    }
} 

public class AndroidLauncher extends AndroidApplication {
    AndroidLauncher android =new AndroidLauncher();

    public void walo() {
        Toast.makeText(getApplicationContext(), "You Don't Have Enough Money",
          Toast.LENGTH_LONG).show();
    }
}

Upvotes: 0

Views: 425

Answers (1)

Deniz Yılmaz
Deniz Yılmaz

Reputation: 1094

Create interface in core, implement this interface in AndroidLauncher and send it to game. So you can call method or pass data to render.

Interface:

    public interface SomeInterface    {
    public void walo();
}

AndroidLauncher:

   public class AndroidLauncher implements SomeInterface{

        @Override
        protected void onCreate() {    

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new MyGdxGame(this), config);

    }

        public void walo() {
          Toast.makeText(getApplicationContext(), "You Don't Have Enough Money",
          Toast.LENGTH_LONG).show();
    }
    }

In game class

  public MyGdxgame(SomeInterface myinterface)           {
            this.myinterface=myinterface;
        }
   public render()            {
            myinterface.walo()
        }

Here's example (my google play service interface) and a link to open source data of my libgdx game.

    public interface PlayServices
{
    public void signIn();
    public void signOut();
    public void rateGame();
    public void unlockAchievement(String str);
    public void submitScore(int highScore);
    public void submitLevel(int highLevel);
    public void showAchievement();
    public void showScore();
    public void showLevel();
    public boolean isSignedIn();
    public void showBannerAd();
    public void hideBannerAd();
    public void showInterstitialAd (Runnable then);
    public void showRewardedVideo();
    public boolean isRewardEarned();
}

You can see ads and video rewards implemented like this.

Github Connect Game

Upvotes: 1

Related Questions