如何在LibGDX项目中显示非页内广告? [英] How to display a Interstitial Ad in a LibGDX project?

查看:114
本文介绍了如何在LibGDX项目中显示非页内广告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一个Flappy Bird GitHub项目并稍微改了一下。
我成功实施了AdMob Banner。

I found a Flappy Bird GitHub project and changed it a little bit. I successfully implemented the AdMob Banner.

但是现在我还想要一个在游戏结束时弹出的非页内广告(当然不是每次都会出现)所以这是我的GitHub项目: https://github.com/DaFaack/FlappyBibi

But now I also want a Interstitial Ad which pops up when the Game is over (not every time of course) so here is my GitHub project: https://github.com/DaFaack/FlappyBibi

请解释我怎么做,因为我在网上找不到好的解释。

Please explain me how to do it because I can't find a good explanation on the internet.

我想在run()方法中显示广告。你可以在核心包中找到它 - > GameplayScreen.java 文件 - > renderPlaying()方法然后 run()方法。

I want to display the ad in the run() method. You can find it in the core package -> GameplayScreen.java File -> renderPlaying() method and then the run() method.

这是我所说的方法:

    private void renderPlaying() {
        if (justTouched) {
            bird.jump();
            justTouched = false;

        }
        updatePipePairs();
        gameplayStage.act();
        uiStage.act();
        checkCollisions();
        if (bird.getState() == Bird.State.DYING) {
            stopTheWorld();


            RunnableAction playWooshAction = Actions.run(new Runnable() {
                @Override
                public void run() {
                    com.pentagames.flappybibi.Assets.playWooshSound();

//Here I want to display the Interstitial Ad!

                }
            });

游戏结束后,如何显示插页式广告?

What do I have to do to display the Interstitial Ad when the game is over?

这是我的AndroidLauncher.java文件:

This is my AndroidLauncher.java file:

package com.pentagames.flappybibi.android;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.pentagames.flappybibi.FlappyGame;

public class AndroidLauncher extends AndroidApplication{

    private static final String TAG = "AndroidLauncher";
    protected AdView adView;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RelativeLayout layout = new RelativeLayout(this);

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        View gameView=initializeForView(new FlappyGame(), config);
        layout.addView(gameView);

        adView = new AdView(this);
        adView.setAdListener(new AdListener(){
            @Override
            public void onAdLoaded(){
                int visibility = adView.getVisibility();
                adView.setVisibility(AdView.GONE);
                adView.setVisibility(AdView.VISIBLE);
                Log.i(TAG, "Ad Loaded...");
            }
        });
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId("ca-app-pub-XXXXXXXXXXXXXX/XXXXXXXXX");

        AdRequest.Builder builder = new AdRequest.Builder();
        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );
        adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        adParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

        layout.addView(adView, adParams);
        adView.loadAd(builder.build());

        setContentView(layout);
    }

    @Override
    protected void onResume() {
        super.onResume();
        adView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        adView.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        adView.destroy();
    }
    }

这就是GameplayScreen文件:

And this the GameplayScreen file:

package com.pentagames.flappybibi;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.RunnableAction;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.viewport.StretchViewport;

public class GameplayScreen extends ScreenAdapter{

    public static final float PIPE_SPACING = 200f;
    public static final int PIPE_SETS = 3;

    protected OrthographicCamera camera;
    protected com.pentagames.flappybibi.FlappyGame game;

    private Stage gameplayStage;
    private Stage uiStage;
    private Label scoreLabel;
    private Label tapToRetry;
    private Label best;
    private Label tapToFlap;
    private Image whitePixel;
    private Image backgroundBuildings;

    private int score;

    private Bird bird;
    private Array<PipePair> pipePairs;

    private com.pentagames.flappybibi.Ground ground;

    private boolean justTouched;

    private Color backgroundColor;

    private State screenState = State.PREGAME;
    private boolean allowRestart = false;

    private enum State {PREGAME, PLAYING, DYING, DEAD}

    public GameplayScreen(com.pentagames.flappybibi.FlappyGame game) {
        this.game = game;

        camera = new OrthographicCamera(com.pentagames.flappybibi.FlappyGame.WIDTH, com.pentagames.flappybibi.FlappyGame.HEIGHT);
        gameplayStage = new Stage(new StretchViewport(com.pentagames.flappybibi.FlappyGame.WIDTH, com.pentagames.flappybibi.FlappyGame.HEIGHT, camera));
        uiStage = new Stage(new StretchViewport(com.pentagames.flappybibi.FlappyGame.WIDTH, com.pentagames.flappybibi.FlappyGame.HEIGHT));
        bird = new Bird();
        bird.setPosition(com.pentagames.flappybibi.FlappyGame.WIDTH * .25f, com.pentagames.flappybibi.FlappyGame.HEIGHT / 2, Align.center);
        bird.addAction(Utils.getFloatyAction());
        bird.setState(Bird.State.PREGAME);

        whitePixel = new Image(com.pentagames.flappybibi.Assets.whitePixel);

        scoreLabel = new Label("0", new Label.LabelStyle(com.pentagames.flappybibi.Assets.fontMedium, Color.WHITE));
        scoreLabel.setPosition(com.pentagames.flappybibi.FlappyGame.WIDTH / 2, com.pentagames.flappybibi.FlappyGame.HEIGHT * .9f, Align.center);
        uiStage.addActor(scoreLabel);

        tapToRetry = new Label("Nochmal?", new Label.LabelStyle(com.pentagames.flappybibi.Assets.fontMedium, Color.WHITE));
        tapToRetry.setPosition(com.pentagames.flappybibi.FlappyGame.WIDTH / 2, 0, Align.top);
        uiStage.addActor(tapToRetry);

        best = new Label("Highscore: ", new Label.LabelStyle(com.pentagames.flappybibi.Assets.fontMedium, Color.WHITE));
        best.setPosition(com.pentagames.flappybibi.FlappyGame.WIDTH / 2, 0, Align.top);
        uiStage.addActor(best);

        tapToFlap = new Label("Fass mich an!", new Label.LabelStyle(com.pentagames.flappybibi.Assets.fontMedium, Color.WHITE));
        tapToFlap.setPosition(com.pentagames.flappybibi.FlappyGame.WIDTH / 2, com.pentagames.flappybibi.FlappyGame.HEIGHT, Align.bottom);
        uiStage.addActor(tapToFlap);

        initBackgroundBuildings();

        pipePairs = new Array<PipePair>();

        ground = new com.pentagames.flappybibi.Ground();
        ground.setPosition(0, 0);

        backgroundColor = Utils.getRandomBackgroundColor();

        // The order actors are added determines the order they are drawn so make sure the background is first
        gameplayStage.addActor(ground);
        gameplayStage.addActor(backgroundBuildings);
        gameplayStage.addActor(bird);

        // Setup the input processor
        initInputProcessor();
    }

    private void initBackgroundBuildings() {
        backgroundBuildings = new Image(com.pentagames.flappybibi.Assets.backgroundBuildings);
        backgroundBuildings.setWidth(com.pentagames.flappybibi.FlappyGame.WIDTH);
        backgroundBuildings.setHeight(backgroundBuildings.getHeight()*2f);
        backgroundBuildings.setY(com.pentagames.flappybibi.Ground.HEIGHT);
    }

    @Override
    public void show() {
        tapToFlap.addAction(Actions.moveToAligned(com.pentagames.flappybibi.FlappyGame.CENTER_X, com.pentagames.flappybibi.FlappyGame.CENTER_Y + 100f, Align.center, .75f, Interpolation.sine));
        com.pentagames.flappybibi.Assets.playWooshSound();
    }

    @Override
    public void render(float delta) {

        Gdx.graphics.getGL20().glClearColor(backgroundColor.r, backgroundColor.g, backgroundColor.b, 1f);
        Gdx.graphics.getGL20().glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

        switch (screenState) {
            case PREGAME:
                updateAndDrawStages();
                break;
            case PLAYING:
                renderPlaying();
                break;
            case DYING:
            case DEAD:
                renderDeadOrDying();
                break;
        }
    }

    private void renderDeadOrDying() {
        if (bird.getState() == Bird.State.DEAD) {
            screenState = State.DEAD;
        }
        updateAndDrawStages();
    }

    private void renderPlaying() {
        if (justTouched) {
            bird.jump();
            justTouched = false;

        }
        updatePipePairs();
        gameplayStage.act();
        uiStage.act();
        checkCollisions();
        if (bird.getState() == Bird.State.DYING) {
            stopTheWorld();

            RunnableAction playWooshAction = Actions.run(new Runnable() {
                @Override
                public void run() {
                    com.pentagames.flappybibi.Assets.playWooshSound();

//Here I want to display the Interstitial Ad!

                }
            });

            SequenceAction actions = Actions.sequence(Actions.delay(1f), playWooshAction, Actions.moveToAligned(com.pentagames.flappybibi.FlappyGame.CENTER_X, com.pentagames.flappybibi.FlappyGame.CENTER_Y, Align.bottom,
                    .75f, Interpolation.sine), Actions.run(new Runnable() {
                @Override
                public void run() {
                    // Allow the player to restart the game once the tap to retry finishes coming up
                    allowRestart = true;
                }
            }));
            tapToRetry.addAction(actions);

            best.setText("Highscore: " + com.pentagames.flappybibi.SavedDataManager.getInstance().getHighScore());
            best.setWidth(best.getTextBounds().width);
            best.setPosition(com.pentagames.flappybibi.FlappyGame.CENTER_X, 0, Align.top);
            best.addAction(Actions.delay(1f, Actions.moveToAligned(com.pentagames.flappybibi.FlappyGame.CENTER_X, com.pentagames.flappybibi.FlappyGame.CENTER_Y, Align.top,
                    .75f, Interpolation.sine)));

            screenState = State.DYING;
        }
        gameplayStage.draw();
        uiStage.draw();
    }

    private void updateAndDrawStages() {
        gameplayStage.act();
        gameplayStage.draw();
        uiStage.act();
        uiStage.draw();
    }

    @Override
    public void resize(int width, int height) {

        camera.setToOrtho(false, width, height);
        com.pentagames.flappybibi.Assets.batch.setProjectionMatrix(camera.combined);
        gameplayStage.getViewport().update(width, height, true);
        uiStage.getViewport().update(width, height, true);

    }

    @Override
    public void dispose() {
        gameplayStage.dispose();
        uiStage.dispose();
    }

    private void checkCollisions() {

        for (int i = 0; i < pipePairs.size; i++) {
            PipePair pair = pipePairs.get(i);
            if (pair.getBottomPipe().getBounds().overlaps(bird.getBounds()) || pair.getTopPipe().getBounds().overlaps(bird.getBounds())) {
                stopTheWorld();
                com.pentagames.flappybibi.SavedDataManager.getInstance().setHighScore(score);
                showWhiteScreen();
            } else if (bird.isBelowGround()) {
                bird.setY(com.pentagames.flappybibi.FlappyGame.GROUND_LEVEL);
                bird.clearActions();
                bird.setToDying();
                showWhiteScreen();
            } else if (bird.isAboveCeiling()) {
                bird.setY(com.pentagames.flappybibi.FlappyGame.HEIGHT - bird.getHeight());
                bird.setToDying();
                showWhiteScreen();
            } else if (pair.getRuby().getBounds().overlaps(bird.getBounds())) {
                score++;
                updateScoreLabel();
                pair.moveCoinOffscreen();
                com.pentagames.flappybibi.Assets.playBingSound();
            }
        }
    }

    private void showWhiteScreen() {
        whitePixel.setWidth(com.pentagames.flappybibi.FlappyGame.WIDTH);
        whitePixel.setHeight(com.pentagames.flappybibi.FlappyGame.HEIGHT);

        gameplayStage.addActor(whitePixel);

        whitePixel.addAction(Actions.fadeOut(.5f));
    }

    private void updateScoreLabel() {
        scoreLabel.setText(String.valueOf(score));
        scoreLabel.setWidth(scoreLabel.getTextBounds().width);
        scoreLabel.setPosition(com.pentagames.flappybibi.FlappyGame.WIDTH / 2, com.pentagames.flappybibi.FlappyGame.HEIGHT * .9f, Align.center);
    }

    private void stopTheWorld() {
        bird.setToDying();
        killPipePairs();
        stopTheGround();
        screenState = State.DYING;

    }

    private void stopTheGround() {
        ground.vel.x = 0;
    }

    private void killPipePairs() {
        for (PipePair pair : pipePairs) {
            pair.getBottomPipe().setState(Pipe.State.dead);
            pair.getTopPipe().setState(Pipe.State.dead);
            pair.getRuby().setVel(0, 0);
        }
    }

    private void updatePipePairs() {
        for (int i = 0; i < pipePairs.size; i++) {
            pipePairs.get(i).update();
        }
    }

    private void addPipes(Stage gameplayStage) {
        for (int i = 0; i < pipePairs.size; i++) {
            gameplayStage.addActor(pipePairs.get(i).getBottomPipe());
            gameplayStage.addActor(pipePairs.get(i).getTopPipe());
            gameplayStage.addActor(pipePairs.get(i).getRuby());
        }
    }

    private void initThirdSetOfPipes() {
        Pipe topPipe = new Pipe();
        Pipe bottomPipe = new Pipe();
        topPipe.getRegion().flip(false, true);
        PipePair pair = new PipePair(topPipe, bottomPipe);
        pair.initThird();

        // add the pair to the list
        pipePairs.add(pair);
    }

    private void initSecondSetOfPipes() {
        Pipe topPipe = new Pipe();
        Pipe bottomPipe = new Pipe();
        topPipe.getRegion().flip(false, true);
        PipePair pair = new PipePair(topPipe, bottomPipe);
        pair.initSecond();

        // add the pair to the list
        pipePairs.add(pair);
    }

    private void initFirstSetOfPipes() {
        Pipe topPipe = new Pipe();
        Pipe bottomPipe = new Pipe();
        topPipe.getRegion().flip(false, true);
        PipePair pair = new PipePair(topPipe, bottomPipe);
        pair.initFirst();

        // add the pair to the list
        pipePairs.add(pair);
    }

    /**
     * Tells libgdx to listen for inputs coming from the InputAdapter we give it
     */
    private void initInputProcessor() {
        Gdx.input.setInputProcessor(new InputAdapter() {
            // We only care about the touch down event
            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button) {

                switch (screenState) {

                    case DYING:
                        justTouched = true;
                        break;

                    case DEAD:
                        if (allowRestart) {
                            game.setScreen(new GameplayScreen(game));
                        }
                        justTouched = true;
                        break;

                    case PLAYING:
                        justTouched = true;
                        break;


                    case PREGAME:
                        justTouched = true;
                        screenState = State.PLAYING;
                        bird.setState(Bird.State.ALIVE);
                        bird.clearActions();
                        tapToFlap.addAction(Actions.moveToAligned(com.pentagames.flappybibi.FlappyGame.CENTER_X, com.pentagames.flappybibi.FlappyGame.HEIGHT, Align.bottom, .75f, Interpolation.sine));
                        initFirstSetOfPipes();
                        initSecondSetOfPipes();
                        initThirdSetOfPipes();
                        addPipes(gameplayStage);
                        gameplayStage.addActor(ground);
                        gameplayStage.addActor(bird);
                        break;

                }
                return true;
            }
        });
    }


}

这是我第一次使用LibGDX,如果你能解释我如何在这个项目中实现插页式广告,那将会很棒。

This is the first time that I'm working with LibGDX, would be great if you can explain me how to implement the Interstitial Ad in this project.

抱歉我的英语不好。

推荐答案

您已经集成了横幅广告,因此无需在项目中注入相关工件。

You already integrated banner Ad so no need to injected dependent artifact in your project.

按照以下步骤进行插页式广告整合。

Follow these steps for Interstitial Ad integration.


  1. AndroidManifest.xml

为非页内广告输入 AdActivity

<activity android:name="com.google.android.gms.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
          android:theme="@android:style/Theme.Translucent" />


  • 在核心模块内创建一个界面

  • Create an interface inside your core module

    public interface AdService {
    
       boolean isInterstitialLoaded();
       void showInterstitial();
    }
    


  • 创建 FlappyGame <的参数化构造函数/ code> class

  • Create a parameterized constructor of FlappyGame class

    public AdService adService;
    
    public FlappyGame(AdService ads){
         adService=ads;
    }
    


  • 实施 AdService 接口到 AndroidLauncher

    public class AndroidLauncher extends AndroidApplication implements AdService {
    
        private static final String AD_UNIT_ID_INTERSTITIAL = "ca-app-pub-XXXXX/XXXXX";
        private InterstitialAd interstitialAd;
    
        @Override
        protected void onCreate (Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
    
             ...
             View gameView=initializeForView(new FlappyGame(this), config);
             ...
    
             interstitialAd = new InterstitialAd(this);
             interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
             interstitialAd.setAdListener(new AdListener() {
                  @Override
                  public void onAdLoaded() {}
    
                  @Override
                  public void onAdClosed() {
                       loadIntersitialAd();
                  }
             });
    
             loadIntersitialAd();
        }
    
        private void loadIntersitialAd(){
    
           AdRequest interstitialRequest = new AdRequest.Builder().build();
           interstitialAd.loadAd(interstitialRequest);
        }
    
      @Override
      public void showInterstitial() {
             runOnUiThread(new Runnable() {
                 public void run() {
                   if (interstitialAd.isLoaded())
                      interstitialAd.show();
                   else
                      loadIntersitialAd();
                 }
             });
       }
    
       @Override
       public boolean isInterstitialLoaded() {
            return interstitialAd.isLoaded();
       }
    }
    


  • GameScreen class

    RunnableAction playWooshAction = Actions.run(new Runnable() {
            @Override
            public void run() {
                com.pentagames.flappybibi.Assets.playWooshSound();
                 game.adService.showInterstitial();
            }
    });
    


  • 我在您的项目中集成了非页内广告,已创建提取请求。您可以合并我的拉取请求。

    I integrated Interstitial Ad in your project, created a pull request for the same. You can merge my pull request.

    这篇关于如何在LibGDX项目中显示非页内广告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆