我找到了Flappy Bird GitHub项目,并对其进行了一些更改.
我成功实施了AdMob标语.
但是现在我还想要一个插页式广告,当游戏结束时d出(当然不是每次都这样),所以这是我的GitHub项目:https://github.com/DaFaack/FlappyBibi
请解释一下该怎么做,因为我在互联网上找不到很好的解释.
我想在run()方法中显示广告.您可以在核心程序包中找到它-> GameplayScreen.java文件-> renderPlaying()方法,然后是run()方法.
这是我正在谈论的方法:
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! } });
游戏结束后,我该怎么做才能显示插页广告?
这是我的AndroIDLauncher.java文件:
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文件:
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,如果您能解释一下如何在此项目中实施插页式广告,那将是非常不错的.
对不起,我的英语不好.
解决方法:
您已经集成了横幅广告,因此无需在项目中注入依赖的工件.
请按照以下步骤进行插页式广告集成.
> 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" />
>在核心模块中创建接口
public interface AdService { boolean isInterstitialLoaded(); voID showInterstitial();}
>创建FlappyGame类的参数化构造函数
public AdService adService;public FlappyGame(AdService ads){ adService=ads;}
>为您的AndroIDLauncher类实现AdService接口
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类
RunnableAction playWooshAction = Actions.run(new Runnable() { @OverrIDe public voID run() { com.pentagames.flappybibi.Assets.playWooshSound(); game.adService.showInterstitial(); }});
我在您的项目中集成了非页内广告,并为它创建了pull request.您可以合并我的请求请求.
总结以上是内存溢出为你收集整理的java-如何在LibGDX项目中显示插页广告?全部内容,希望文章能够帮你解决java-如何在LibGDX项目中显示插页广告?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)