如何在更改屏幕中添加插页式广告项目Libgdx

时间:2015-12-31 01:09:55

标签: java android libgdx admob

我查看了很多关于如何在libgdx中添加插页式广告的教程。但是所有这些都使用了传递接口的构造函数。

我创建了改变屏幕的游戏。 我的课程是: (在AndroidLauncher:Initialize(new GameChanger(),config))

GameChanger扩展游戏......(setscreen Mainmenu)---->

实现屏幕并且具有在Gamechanger中传递的构造函数的主菜单...(setscreen MyGDXGame) - >

MyGDXGame实现屏幕并具有在Gamechanger中传递的构造函数 - > ...(setscreen End)

(我需要在MyGdxGame和End之间显示广告)

结束实现屏幕并具有在Gamechanger中传递的构造函数

有没有办法在不使用构造函数的情况下在两个屏幕之间添加广告 还是有不同的方式?

请回复? 对不起任何代码和语法错误。 感谢

这是我的AndroidLauncher:

公共类AndroidLauncher扩展了AndroidApplication {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    initialize(new Gamekeeper(),config);
}

}

这是我的GameKeeper类,它扩展了Game: 公共类Gamekeeper扩展了游戏{

public SpriteBatch batch;

@Override
public void create(){
    batch = new SpriteBatch();
    this.setScreen(new MainMenu(this));
}
@Override
public void render(){
    super.render();
}
@Override
public void dispose(){
    batch.dispose();
}

}

这是我的MainMenu类,它实现了Screen:

protected MainMenu(Gamekeeper gam) {
    game = gam;
batch = new SpriteBatch();
Gdx.input.setInputProcessor(this);

}

public void render(float delta) {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();
    backgroundSound.setLooping(true);
    backgroundSound.play();
    backgroundSprite.draw(batch);
    titleFont.draw(batch, title, playButton.getX(), Gdx.graphics.getHeight() - 100);
    scoreFont.draw(batch, "Top Score: " + score, Gdx.graphics.getWidth() / 3 - 20, Gdx.graphics.getHeight() / 2 + 30);
    playButton.draw(batch);
    batch.end();
}

public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    if(play.contains(screenX,screenY)){
       buttonClick.play();
        backgroundSound.stop();
        mainTheme.setLooping(true);
        mainTheme.play();
       play.setPosition(-500,-500);
       game.setScreen(new MyGdxGame(game));
    }
    return true;
}

其他类大多相似

1 个答案:

答案 0 :(得分:0)

通过实施界面添加Intersitial Ads不是问题。在您的核心项目文件夹中创建接口文件。

public interface PlayServices
{
....//some other playservices methods;
public void showOrLoadInterstital();
}

在AdnroidLauncher.java类中实现创建的PlayServices接口。

public class AndroidLauncher extends AndroidApplication implements PlayServices {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

    adView = new InterstitialAd(this);
    adView.setAdUnitId(YOUR ID OF ADMOB);
    adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
           //all of these toasts added for the debug reason, after successfully
           //implementing method you can remove them
           Toast.makeText(getApplicationContext(), "Finished Loading Interstitial", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdClosed() {
            Toast.makeText(getApplicationContext(), "Closed Interstitial", Toast.LENGTH_SHORT).show();
        }
    });

    requestNewInterstitial();

    initialize(new Gamekeeper(this),config);
}

@Override
public void showOrLoadInterstital() {
    try {
        runOnUiThread(new Runnable() {
            public void run() {

                if (adView.isLoaded()) {
                    adView.show();
                    requestNewInterstitial();
                    Toast.makeText(getApplicationContext(), "Showing Interstitial", Toast.LENGTH_SHORT).show();
                } else {
                    AdRequest interstitialRequest = new AdRequest.Builder().build();
                    adView.loadAd(interstitialRequest);
                    Toast.makeText(getApplicationContext(), "Loading Interstitial", Toast.LENGTH_SHORT).show();
                }

            }
        });
    } catch (Exception e) {
    }
}

}

` 然后在你的核心java文件中你需要处理PlayServices。

public class Gamekeeper extends Game{
    public static PlayServices playServices;

    public Gamekeeper (PlayServices playServices){
        this.playServices = playServices;
    }
}

因此,无论您何时打电话给广告,都可以使用此广告:

Gamekeeper.playServices.showOrLoadInterstital();

多数民众赞成。 只是不要忘记制作一些proper thing

1.下载Google Play服务。

2.包含并参考Google Play服务库项目。

3.修改清单。