我在LibGDX上玩游戏。根据这个 http://www.norakomi.com/tutorial_admob_part2_banner_ads1.php 指令我在AndroidLauncher.java文件中创建了necessery方法。在由AndroidLauncher.java生成的核心文件中,我创建了控制器并且还创建了接口java文件 (http://www.norakomi.com/tutorial_admob_part2_banner_ads2.php)。 问题是我的游戏有几个相互延伸的类,我想用来显示adMob的相应条件不是那个方法"初始化"给出了#34;这个"来自AndroidLauncher.java文件。但是只能从AndroidLauncher.java下载并提供adMob请求,因为其他类都在自己的游戏视图中。
如何解决这个问题?
这是AndroidLauncher.java的基本代码
public class AndroidLauncher extends AndroidApplication implements AdsController {
private static final String BANNER_AD_UNIT_ID = "ca-app-pub-3940256099942544/6300978111";
private static final String INTERSTITIAL_AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712";
AdView bannerAd;
InterstitialAd interstitialAd;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// Create a gameView and a bannerAd AdView
View gameView = initializeForView(new Stork2016(this), config);
setupBanner();
setupInterstitial();
// Define the layout
RelativeLayout layout = new RelativeLayout(this);
layout.addView(gameView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(bannerAd, params);
setContentView(layout);
config.useCompass = false;
config.useAccelerometer = false;
public void setupBanner() {
bannerAd = new AdView(this);
//bannerAd.setVisibility(View.VISIBLE);
//bannerAd.setBackgroundColor(0xff000000); // black
bannerAd.setAdUnitId(BANNER_AD_UNIT_ID);
bannerAd.setAdSize(AdSize.SMART_BANNER);
}
public void setupInterstitial() {
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(INTERSTITIAL_AD_UNIT_ID);
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest ad = builder.build();
interstitialAd.loadAd(ad);
@Override
public void showInterstitialAd(final Runnable then) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (then != null) {
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
Gdx.app.postRunnable(then);
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest ad = builder.build();
interstitialAd.loadAd(ad);
}
});
}
interstitialAd.show();
}
});
}
@Override
public boolean isWifiConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return (ni != null && ni.isConnected());
}
@Override
public void showBannerAd() {
runOnUiThread(new Runnable() {
@Override
public void run() {
bannerAd.setVisibility(View.VISIBLE);
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest ad = builder.build();
bannerAd.loadAd(ad);
}
});
}
@Override
public void hideBannerAd() {
runOnUiThread(new Runnable() {
@Override
public void run() {
bannerAd.setVisibility(View.INVISIBLE);
}
});
}
}
然后我们有文件Stork2016.java,我们在其中创建AdsController,以便能够在AndroidLauncher.java中使用添加方法。
private AdsController adsController;
public Stork2016(AdsController adsController){
this.adsController = adsController;
}
@Override
public void create () {
adsController.showBannerAd();
batch = new SpriteBatch();
gsm = new GameStateManager();
music = Gdx.audio.newMusic(Gdx.files.internal("music.mp3"));
music.setLooping(true);
music.setVolume(0.5f);
music.play();
Gdx.gl.glClearColor(1, 0, 0, 1);
gsm.push(new MenuState(gsm));
}
我们还有接口java文件AdsController.java
public interface AdsController {
public void showBannerAd();
public void hideBannerAd();
public void showInterstitialAd (Runnable then);
public boolean isWifiConnected();
}
所以,正如我们在Stork2016中看到的那样,我们有" gsm.push(新的MenuState(gsm));"在MenuState.java中我有" gsm.set(new PlayState(gsm));"。在PlayState.java中有代码的一部分:
@Override
public void update(float dt) {
handleInput();
updateGround();
....
if (tube.collides(bird.getBounds()))
gsm.set(new GameOver(gsm));
...
}
}
camera.update();
}
条件"如果"我想使用上面的代码来显示插页式adMob。但这是不可能的,因为从AndroidLauncher.java获取方法的控制器只能在Stork2016.java中创建。而且在AndroidLauncher.java中也有 查看gameView = initializeForView(new Stork2016(this),config); 转移"这个"到Stork2016,控制器在哪里。
答案 0 :(得分:0)
在我的AndroidLauncher
活动中,我开始游戏并初始化Insterstitial
广告。然后我初始化我在游戏中调用的interface,以触发显示/隐藏插页式广告。
例如我在我的界面监听器中有方法showInterstitialAd(),那么我在Android上的实现将是:
@Override
public void showCoverAd() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
}
});
}
在iOS-MOE上:
@Override
public void showCoverAd() {
if (gadInterstitial.isReady()) {
gadInterstitial.presentFromRootViewController(uiViewController);
}
}
因此,您需要确保接口侦听器知道插页式广告,例如AndroidLauncher implements MyGameEventListener
答案 1 :(得分:0)
在我的案例界面中,AdsController.java在AndroidLauncher.java中实现:
public class AndroidLauncher extends AndroidApplication implements AdsController { ...
然后通过这部分代码:
View gameView = initializeForView(new Stork2016(this), config);
我们发送"这个"到新类Strork2016.java。 在Stork2016.java类中,我创建了构造函数:
private AdsController adsController;
public Stork2016(AdsController adsController){
this.adsController = adsController;
}
允许我们使用接口AdsController.java中的方法。
但仅限于本课程Stork2016。如果我想在另一个类中使用它:
gsm.push(new MenuState(gsm));
这是不可能的,这就是问题所在。
答案 2 :(得分:0)