我一直在用他的书中使用Mario Zechner的开发风格(框架)在LibGDX中开发游戏。他在书中使用的一个例子是名为Mr.Nom的游戏。我正在尝试将AdMob广告添加到游戏中,但是遵循更常见的AdMob / LibGDX教程很困难,因为它与Mario Zechner的框架不同。
您可以在此处查看Mr.Nom的源代码:https://code.google.com/p/beginnginandroidgames2/source/browse/trunk/ch06-mr-nom/src/com/badlogic?spec=svn13&r=6#badlogic%253Fstate%253Dclosed
所以,我做了一些进一步的研究,看看是否还有其他人将AdMob整合到像Mr.Nom这样的游戏中有类似的麻烦并找到了这两个链接:
http://www.badlogicgames.com/forum/viewtopic.php?f=21&t=982
http://www.badlogicgames.com/forum/viewtopic.php?f=21&t=1548
我开始做Saurav提供的一些补充,但我现在收到错误,我不知道错误告诉我的是什么。
以下是错误:
http://pastebin.com/JZ9Q1kb4
这是我的AndroidGame.java:
package com.badlogic.androidgames.framework.impl;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.badlogic.androidgames.framework.FileIO;
import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Input;
import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.shutproject.MainMenu;
import com.badlogic.androidgames.shutproject.R;
import com.badlogic.androidgames.shutproject.R.id;
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;
public abstract class AndroidGame extends Activity implements Game, IActivityRequestHandler {
AndroidFastRenderView renderView;
Graphics graphics;
Input input;
FileIO fileIO;
Screen screen;
WakeLock wakeLock;
/* Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = "privateofc";
private static final String PERS_MOB_ID = "LG-LS980-060df4789011d477"; // test on lgg2
private AdView adView;
private final static int SHOW_ADS = 1;
private final static int HIDE_ADS = 0;
protected Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case SHOW_ADS:
{
adView.setVisibility(View.VISIBLE);
break;
}
case HIDE_ADS:
{
adView.setVisibility(View.GONE);
break;
}
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
int frameBufferWidth = isLandscape ? 480 : 320;
int frameBufferHeight = isLandscape ? 320 : 480;
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
frameBufferHeight, Config.RGB_565);
float scaleX = (float) frameBufferWidth
/ getWindowManager().getDefaultDisplay().getWidth();
float scaleY = (float) frameBufferHeight
/ getWindowManager().getDefaultDisplay().getHeight();
// *** This line gets you the Game view
renderView = new AndroidFastRenderView(this, frameBuffer);
// *** Next, you create the AdMob view
// *** Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
// *** String MY_BANNER_UNIT_ID = "abcde123456"; whatever id you get from AdMob
AdView adView = new AdView(this);
adView.setAdUnitId(AD_UNIT_ID);
adView.setAdSize(AdSize.BANNER);
// *** Now, you need to have both these views on the screen, which means you need a layout.
// *** I used a relative layout, there may be ways to make this work with other layouts
RelativeLayout layout = new RelativeLayout(this);
// *** First, add the game view to the layout. This will use default parameters, which means
// *** the Game view will take up the whole screen, which is usually what you want. Doing this
// *** first ensures the game view is under the ad view. Do it in the reverse sequence, and the
// ** game view will hide the ad view, which is not what you want
layout.addView(renderView);
// *** Then, prepare the ad view for where you want it to go on the layout. I wanted the ad
// *** to be on the bottom right corner. First, you want the view for the ad to only be as large
// *** as needed. So use WRAP_CONTENT instead of FILL_PARENT
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// *** Now set up the rules that tell the layout how to align this view. In my case, I want it
// *** on the bottom right
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// *** Then add the view to the layout, using this version of the function that lets you specify
// *** the layout parameters
layout.addView(adView, params);
// *** And finally, you want the activity to use your whole layout, and not just he game view, so:
setContentView(layout);
graphics = new AndroidGraphics(getAssets(), frameBuffer);
fileIO = new AndroidFileIO(this);
input = new AndroidInput(this, renderView, scaleX, scaleY);
screen = getStartScreen();
// *** Delete this line - this will point the activity to the game view, and you'll lose all the work you did
// *** setting up the layout
//----setContentView(renderView);----
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
}
public Context context() {
return this.getApplicationContext();
}
@Override
public void onResume() {
super.onResume();
wakeLock.acquire();
screen.resume();
renderView.resume();
}
@Override
public void onPause() {
super.onPause();
wakeLock.release();
renderView.pause();
screen.pause();
if (isFinishing())
screen.dispose();
}
public Input getInput() {
return input;
}
public FileIO getFileIO() {
return fileIO;
}
public Graphics getGraphics() {
return graphics;
}
public void setScreen(Screen screen) {
if (screen == null)
throw new IllegalArgumentException("Screen must not be null");
this.screen.pause();
this.screen.dispose();
screen.resume();
screen.update(0);
this.screen = screen;
}
public Screen getCurrentScreen() {
return screen;
}
}
有人能指出我正确的方向吗?
答案 0 :(得分:0)
您的错误表明您正在引用Google Play服务中的Admob类,但这些类不在您的应用程序的类路径中。
您需要将Google Play服务库添加到您的项目中。