好吧即时通过开始页面,主要游戏和屏幕上的游戏都在我的主游戏环中,制作一个类似飞扬鸟的小游戏我通过更改可靠的名为gameState来切换正在画布上绘制的图像,即:gameState == 0(开始页面),gameState == 1(主游戏),gameState == 2(gameover屏幕)一切正常,但问题是当我尝试从admob加载广告时它失败了循环正在运行,即使从我的主要活动调用广告请求,包含我的游戏循环的客户surfaceView也在其自己的单独步骤中运行
主要活动类
@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());
}
gameloop 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 run方法中移除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
答案 0 :(得分:0)
最终不得不使用.tread wait(),直到广告加载仍然不是100%满意,但它可以正常工作