admob 不在游戏循环中 运行
admob not running in game loop
好的,我正在制作一个有点像 flappy bird 的小游戏,因为开始页面、主游戏和游戏结束屏幕都在我的主游戏循环中,我将在 canvas 上绘制的图像切换为更改一个名为 gameState 的变量,即:gameState == 0(起始页),gameState == 1(主游戏),gameState == 2(游戏结束屏幕)一切正常,但问题是当我尝试加载广告时从 admob 它在循环 运行ning 时失败,即使广告请求是从我的主 activity 调用的,并且包含我的 gameloop 的客户 surfaceView 是 运行 在它自己单独的胎面
主要activityclass
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
adView = new AdView(this);
adView.setAdUnitId(unitId_Banner);
adView.setAdSize(AdSize.SMART_BANNER);
sView = new SView(this, this.getResources());
RelativeLayout mainLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mainLayout.addView(sView);
mainLayout.addView(adView, adParams);
adView.bringToFront();
setContentView(mainLayout);
adView.setVisibility(View.VISIBLE);
adView.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());
}
游戏循环class
public void run(){
while(running){
if(!holder.getSurface().isValid()){
continue;
}
canvas = null;
try{
canvas = holder.lockCanvas();
synchronized(holder){
startTime = System.currentTimeMillis();
framesSkipped = 0;
update();
draw(canvas);
timeDiff = System.currentTimeMillis() - startTime;
sleepTime = (int)(FRAME_PERIOD - timeDiff);
if(sleepTime > 0){
try{
Thread.sleep(sleepTime);
}catch(InterruptedException e){}
}
while(sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS){
update();
sleepTime += FRAME_PERIOD;
framesSkipped++;
}
}
}finally{
if(canvas != null){
holder.unlockCanvasAndPost(canvas);
}
}
}
}
如果我要删除 gameloop 运行 方法中的 while 循环,广告加载正常任何人都可以帮助我恳求
03-04 02:47:30.997: I/Ads(277): Starting ad request.
03-04 02:47:58.218: W/Ads(277): There was a problem getting an ad response. ErrorCode: 0
03-04 02:47:58.227: W/Ads(277): Failed to load ad: 0
最终不得不 use.tread wait() 直到广告加载仍然不是 100% 满意但有效
好的,我正在制作一个有点像 flappy bird 的小游戏,因为开始页面、主游戏和游戏结束屏幕都在我的主游戏循环中,我将在 canvas 上绘制的图像切换为更改一个名为 gameState 的变量,即:gameState == 0(起始页),gameState == 1(主游戏),gameState == 2(游戏结束屏幕)一切正常,但问题是当我尝试加载广告时从 admob 它在循环 运行ning 时失败,即使广告请求是从我的主 activity 调用的,并且包含我的 gameloop 的客户 surfaceView 是 运行 在它自己单独的胎面
主要activityclass
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
adView = new AdView(this);
adView.setAdUnitId(unitId_Banner);
adView.setAdSize(AdSize.SMART_BANNER);
sView = new SView(this, this.getResources());
RelativeLayout mainLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mainLayout.addView(sView);
mainLayout.addView(adView, adParams);
adView.bringToFront();
setContentView(mainLayout);
adView.setVisibility(View.VISIBLE);
adView.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());
}
游戏循环class
public void run(){
while(running){
if(!holder.getSurface().isValid()){
continue;
}
canvas = null;
try{
canvas = holder.lockCanvas();
synchronized(holder){
startTime = System.currentTimeMillis();
framesSkipped = 0;
update();
draw(canvas);
timeDiff = System.currentTimeMillis() - startTime;
sleepTime = (int)(FRAME_PERIOD - timeDiff);
if(sleepTime > 0){
try{
Thread.sleep(sleepTime);
}catch(InterruptedException e){}
}
while(sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS){
update();
sleepTime += FRAME_PERIOD;
framesSkipped++;
}
}
}finally{
if(canvas != null){
holder.unlockCanvasAndPost(canvas);
}
}
}
}
如果我要删除 gameloop 运行 方法中的 while 循环,广告加载正常任何人都可以帮助我恳求
03-04 02:47:30.997: I/Ads(277): Starting ad request.
03-04 02:47:58.218: W/Ads(277): There was a problem getting an ad response. ErrorCode: 0
03-04 02:47:58.227: W/Ads(277): Failed to load ad: 0
最终不得不 use.tread wait() 直到广告加载仍然不是 100% 满意但有效