如何在玩家切换到游戏结束屏幕X次后展示插页式广告?

时间:2020-04-23 00:16:22

标签: java libgdx admob

因此,我是AdMob的新手,我想弄清楚在玩家死亡并在屏幕上播放X次后,如何显示插页式广告。我在AndroidLauncher类中设置了AdMob,但是其他类没有ad变量。这是我的AndroidLauncher类当前的样子。如果有帮助,则将游戏设置为游戏状态,0表示游戏开始之前,1表示当前正在玩,2和3均为“游戏结束”状态,将玩家发送到屏幕上的游戏。

            @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() {


            }






        }); ```

1 个答案:

答案 0 :(得分:0)

我不确定这是否是最好的方法,但是我可以解释一下这样做的方法。这可能会有帮助。

1)创建广告服务接口

public interface AdService {

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

}

2)从您的AndroidLauncher类实现此接口,并使用此广告接口为基本Game类构造一个构造函数。从您的androidlauncher类中,引用此构造函数。还生成接口类。 (我只发布相关部分)。

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)想要显示广告时调用它。

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

4)要在X时间后展示广告,只需设置一个计时器即可。