简体   繁体   中英

libGDX/Android Studio AdMob code into other classes

first time posting to this website. I'm new to libGDX and java. I have an intermediate knowledge of c++. My Issue is that my AdMob code only works in my Android Launcher Class (not sure if this is intended or if i'm missing something. I can't seem to import the libraries into other classes) Then I'm having trouble using the AndroidLauncher code where I want to.

The game currently displays an Ad at the correct size when the game starts up. I would like to call the ad (it is currently a banner ad. I eventually want to change it to interstitial. I understand the concepts are somewhat different.) when my else if isGameOver() is used (GameRenderer Class). So that when the hero dies, it displays the Ad. I won't worry about hiding it when the game restarts, since the user can close the eventual interstitial ad themselves. I understand that this is probably more of a logic and lack of knowledge issue, than an issue with AdMob and libGDX. But I've researched a good amount and can't seem to find any sources that make enough sense for me to execute on. If you have any resources, or you can help me understand my issue, I would greatly appreciate it. If I forgot to put any information you need, feel free to ask for it. I will be checking this post regularly.

Android Launcher:

package com.wearnoble.game;

import android.os.Bundle;
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.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

public class AndroidLauncher extends AndroidApplication {
private InterstitialAd mInterstitialAd;

private static final String adUnitId="ca-app-pub-
XXXXXXXXXXXXXXXXXXXX";
private AdView adView;
InterstitialAd interstitialAd;

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

    RelativeLayout layout = new RelativeLayout(this);
    RelativeLayout.LayoutParams params = new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
RelativeLayout.LayoutParams.MATCH_PARENT);
    layout.setLayoutParams(params);

    View gameView=initializeForView(new FinalDemo(), config);

    RelativeLayout.LayoutParams gameViewParams = new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
RelativeLayout.LayoutParams.WRAP_CONTENT);
    gameViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    gameViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

    gameView.setLayoutParams(gameViewParams);
    layout.addView(gameView);

    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(adUnitId);

    AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
    adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
    adView.loadAd(adRequestBuilder.build());

    RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    topParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
    topParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    layout.addView(adView, topParams);
    adView.setBackgroundColor(android.graphics.Color.TRANSPARENT);

    setContentView(layout);

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-4578173480768480/4050414865");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());








}

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

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

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

}

GameWorld:

public class GameWorld {
public int score=0;
private float runTime=0;
private GameState currentState;
private Hero hero;
private ScrollHandler scroller;
private Rectangle ground;
private int midPointY;
private GameRenderer renderer;



public enum GameState {

   MENU, READY, RUNNING, GAMEOVER, HIGHSCORE

}
public GameWorld(int midPointY){
    currentState=GameState.MENU;
    this.midPointY=midPointY;
    hero=new Hero(33,midPointY-5,17,12);
    scroller = new ScrollHandler(this, midPointY + 66);
    ground=new Rectangle(0, midPointY + 66, 137, 11);


}
public void update(float delta) {
    runTime += delta;

    switch (currentState) {
        case READY:
        case MENU:
            updateReady(delta);
            break;

        case RUNNING:
            updateRunning(delta);
            break;
        default:
            break;
    }}
private void updateReady(float delta) {
    hero.updateReady(runTime);
    scroller.updateReady(delta);

}


public void updateRunning(float delta){
    // Add a delta cap so that if our game takes too long
    // to update, we will not break our collision detection.
    if (delta > .15f) {
        delta = .15f;
    }
    hero.update(delta);
    scroller.update(delta);

    if (scroller.collides(hero)&&hero.isAlive()) {
        // Clean up on game over
        scroller.stop();
        hero.die();
        renderer.prepareTransition(255,255,255,.3f);

        //AssetLoader.dead.play();
    }
    //if (scroller.collides(hero) && hero.isAlive()) {
       // scroller.stop();
       // hero.die();
       // AssetLoader.dead.play();

    if  (Intersector.overlaps(hero.getBoundingCircle(), ground)) {
        if (hero.isAlive()){
            //AssetLoader.dead.play();
            renderer.prepareTransition(255,255,255,.3f);
            hero.die();
        }
        scroller.stop();
        hero.decelerate();
        currentState = GameState.GAMEOVER;

        if (score > AssetLoader.getHighScore()) {
            AssetLoader.setHighScore(score);
            currentState = GameState.HIGHSCORE;
        }



}}


public boolean isHighScore() {
return currentState == GameState.HIGHSCORE;
}
public boolean isReady() {
    return currentState == GameState.READY;}
public void start() {
    currentState = GameState.RUNNING;
}
public void restart(){
score = 0;
hero.onRestart(midPointY - 5);
scroller.onRestart();
ready();
}
public boolean isGameOver() {
    return currentState == GameState.GAMEOVER;
}
public Hero getHero(){
    return hero;
}
public ScrollHandler getScroller(){
    return scroller;
}
public int getScore(){
    return score;
}
public void addScore(int increment) {
    score += increment;
}
public int getMidPointY(){
return midPointY;
}
public void ready() {
    currentState = GameState.READY;
    renderer.prepareTransition(0,0,0,1f);
}
public boolean isMenu() {
    return currentState == GameState.MENU;
}
public boolean isRunning() {
    return currentState == GameState.RUNNING;
}
public void setRenderer(GameRenderer renderer){
    this.renderer = renderer;
}

}

And this is the part from the GameRenderer class that is relevant.

if (myWorld.isRunning()) {
        drawHero(runTime);
        drawScore();
    } else if (myWorld.isReady()) {
        drawHero(runTime);
        drawScore();
    } else if (myWorld.isMenu()) {
        drawHeroCentered(runTime);
        drawMenuUI();
    } else if (myWorld.isGameOver()) {
        drawScoreboard();
        drawHero(runTime);
        drawGameOver();
        drawRetry();
    } else if (myWorld.isHighScore()) {
        drawScoreboard();
        drawHero(runTime);
        drawHighScore();
        drawRetry();
    }

In LibGDX, the core module is generally separated from Android module as a dependency, so you cannot directly call the functions of Android module from the core module.

So the solution would be to create an interface with name AdLoader in the core module and add function declarations like showAd() and hideAd() .

Then make your class Android Launcher implement the interface and pass it as a constructor in FinalDemo class of the core module and save the reference of AdLoader, then you can call the interface methods from the core module.

You can't access Android APIs inside core module. Core module is generic part for all platforms.

By interfacing you can use access platform specific code.

Integration of AdMob Ads in LibGDX projects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM