简体   繁体   中英

How do I implement interstitial ads to show after X amount of times a player switches to the gameover screens?

So, I am new to AdMob and I am trying to figure out how to display an interstitial ad after the player dies and goes to the game over screen X amount of times. I have my AdMob set up in my AndroidLauncher class, however, my other classes do not have the ad variables. this is what my AndroidLauncher class looks like currently. If it helps, my game is set up with game states, 0 is before game starts, 1 is currently playing, and 2 and 3 are both game over states that send the player to the game over screen.

            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {}
        });
        ad = new InterstitialAd(this);
        ad.setAdUnitId("ca-app-pub-2188258702xxxxxxxxxxx");
        ad.loadAd(new AdRequest.Builder().build());

        ad.setAdListener(new AdListener(){

            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.

                if (ad.isLoaded()) {
                    ad.show();
                }

            }
            @Override
            public void onAdFailedToLoad(int errorCode) {
                // Code to be executed when an ad request fails.

            }

            @Override
            public void onAdOpened() {

            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.

            }

            @Override
            public void onAdClosed() {


            }






        }); ```

I am not sure if this is the best way but I can explain my way of doing it. This may help.

1) Create an Interface for Adservice

public interface AdService {

void showOrLoadInterstital();
void showBanner();
void hideBanner();
void showRewardedAd();
boolean hasVideoLoaded();
boolean checkAdWatched();

}

2) Implement this interface from your AndroidLauncher class and have a constructor of your base Game Class with this ad interface. From your androidlauncher class, refer to this constructor. Also generate the interface classes. ( I am only posting relevant parts).

public class AndroidLauncher extends AndroidApplication implements AdService {

@Override
protected void onCreate(Bundle savedInstanceState) {
// rest of the code here

setUpInterstitial();

View gameView = initializeForView(new MainGameClass(this), config);
}

private void setUpInterstitial() {
    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
    loadIntersitialAd();
}

private void loadIntersitialAd() {
    AdRequest.Builder builder = new AdRequest.Builder();
    AdRequest interstitialRequest = builder.build();
    interstitialAd.loadAd(interstitialRequest);
}

@Override
public void showOrLoadInterstital() {
    runOnUiThread(new Runnable() {
        public void run() {
            interstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdClosed() {
                    loadIntersitialAd();
                }

                @Override
                public void onAdFailedToLoad(int i) {
                    loadIntersitialAd();
                }

            });
            interstitialAd.show();
            adWatched=false;
        }
    });

}

}

3) Call the ad when you want to show it.

            if (MainGameClass.adService != null) {
                RunnableAction playWooshAction = Actions.run(new Runnable() {
                    @Override
                    public void run() {
                        MainGameClass.adService.showOrLoadInterstital();
                    }
                });
                playWooshAction.run();
            }

4) For showing ad after X amount of time, just set a timer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM