Question

I have Loading\Logo screen and MainMenu screen in my LibGDX game. And I want to show an interstitial ad between these screens.

I have managed to do this with the following code, but the problem is: after logo screen disappears, main menu screen appears for a short second and only then ad is shown - this is what annoys me.

I want logo screen to hide, then ad to be shown and only then main menu screen to appear (black screens are ok in between). Can you advise me how to get there?

My code now is as follows:

Main class just launches logo screen:

public class Colorem extends Game {

        //...   

    IActivityRequestHandler h_AndroidActivity;

    public void create() {  

    //init vars...

        //set logo screen
        this.setScreen(new LogoScreen(this));
    }

        //...   
}

Logo screen loads textures using AssetManager and load main menu screen:

public class LogoScreen implements Screen {
    private boolean bLoading = false,
            bLoadingFinished = false;

    public LogoScreen(final Colorem game) {
        this.game = game;

        // ...

    }


    @Override
    public void render(float delta) {

        //draw logo...

            //keep loading the resources calling assetManager.update()
            if(UI.assetManager.update()) {


                game.setScreen(new MainMenuScreen(game));


           }
        }

    }

Main menu class shows Ad when shown:

public class MainMenuScreen implements Screen {

 //...

        @Override
    public void show() {    
            game.h_AndroidActivity.showInterstitialAd_InGame();
    }


}

I have also tried calling Ad from hide() method of LogoScreen, but still I cannot get clean screen ->Ad -> screen slideshow.

Was it helpful?

Solution

okey. You need to implement AdListener in your MainActivity
public class MainActivity extends AndroidApplication implements AdListener.
This will give you 5 methods

@Override
public void onDismissScreen(Ad arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onLeaveApplication(Ad arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onPresentScreen(Ad arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onReceiveAd(Ad arg0) {
    // TODO Auto-generated method stub

}

Show your InterstitialAdd when all assets are loaded in your LogoScreen . Now show your MainMenuScreen inside onDismissScreen(Ad arg0) method shown above. Also do remember to add listner to your interstitials add interstitial.setAdListener(this);

Incase game.setScreen(new MainMenuScreen(game)); does not work inside onDismissScreen(Ad arg0) then you need to take a boolean and use it to show mainmenuScreen() when onDismissScreen() is called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top