如何在libGdx中的多个用途之间设置广告间广告之间的时间间隔

时间:2018-02-18 21:09:25

标签: libgdx ads

我正在构建一个简单的Ketchapp风格游戏,我想在玩家死亡时出现插页式广告。

这是我正在使用的当前类,当游戏开始时使用构造函数,并且当玩家死亡时运行adCheck方法。

public class GameOverAd {
    private static final int RESET = -1;

    private static long startTime = RESET;
    private static long currentTime;
    private static int secondsPassedSinceLastAd;

    public GameOverAd(){
        if (startTime == RESET){
            startTime = System.currentTimeMillis();
            //first time showOrLoadInterstitial() runs it will load the ad
            //second time showOrLoadInterstitial() runs it will show the ad
            GameMain.handler.showOrLoadInterstitial();//loads
        }
    }

    public void adCheck(){
        currentTime = System.currentTimeMillis();
        secondsPassedSinceLastAd = (int)((currentTime - startTime) /1000);
        if(secondsPassedSinceLastAd >= 20){//seconds between ads
            showAd();
        }
        else{

        }
    }

    public void showAd(){
        GameMain.handler.showOrLoadInterstitial();//shows add
        startTime = RESET;
    }
}

只要玩家没有关闭应用程序,此代码就可以正常工作,当玩家死亡时使用250秒后显示添加,但问题是如果玩家在249秒后退出,则计时器重置并且用户永远不会看到广告。如何让计时器从它停止的同一个地方继续计数?

1 个答案:

答案 0 :(得分:1)

您必须保存关闭时留下的时间,并在应用启动时将其读回。我建议使用Preferences类来实现这个