2016-06-11 94 views
2

我一直试图在我的andengine开发游戏中显示插页式广告,但无法显示。不过,我已成功整合了横幅广告。我更喜欢使用编码进行布局。以下是我的GameActivity.java和Gamescene.java类,请帮助理解根错误在哪里?无法在andengine中使用admob显示插页式广告

public class GameActivity extends BaseGameActivity 
{ 
    private SmoothCamera camera; 
    public Music music;` 

    public static int flag=0; 


    @Override 
    public Engine onCreateEngine(EngineOptions pEngineOptions) 
    { 
     return new LimitedFPSEngine(pEngineOptions, 60); 
    } 

    public EngineOptions onCreateEngineOptions() 
    { 
     camera = new SmoothCamera(0, 0, 720, 1280,5000000,5000000,1); 
     EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(), this.camera); 
     engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true); 
     engineOptions.getRenderOptions().getConfigChooserOptions().setRequestedMultiSampling(true); 
     engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON); 
     engineOptions.getTouchOptions().setNeedsMultiTouch(true); 
     return engineOptions; 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 
     if (keyCode == KeyEvent.KEYCODE_BACK) 
     { 
      SceneManager.getInstance().getCurrentScene().onBackKeyPressed(); 
     } 
     return false; 
    } 

    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException 
    { 
     ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager()); 
     pOnCreateResourcesCallback.onCreateResourcesFinished(); 


     try 
     { 
      music = MusicFactory.createMusicFromAsset(mEngine.getMusicManager(), this,"music/music.ogg"); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 

    } 

    AdView adView ;@Override 
    @SuppressLint("NewApi") 
    protected void onSetContentView() { 


     final FrameLayout frameLayout = new FrameLayout(this); 
     final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(
       FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT, Gravity.FILL); 
     final FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, 
       FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER | Gravity.TOP); 

     adView = new AdView(this); 
     adView.setAdUnitId("admobid"); 
     //adView.setAdUnitId("admobid"); 
     adView.setAdSize(AdSize.); 

     adView.setVisibility(AdView.VISIBLE); 
     adView.refreshDrawableState(); 

     AdRequest adRequest = new AdRequest.Builder().build(); 
     adView.loadAd(adRequest); 

     if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { 
      adView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 
     } 


     this.mRenderSurfaceView = new RenderSurfaceView(this); 
     mRenderSurfaceView.setRenderer(mEngine, this); 



     final FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(
       android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT); 
     surfaceViewLayoutParams.gravity = Gravity.CENTER; 

     frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams); 

     adView.setAdListener(new AdListener() { 
      public void onAdLoaded(){ 

       if(flag==0) 
       {flag=1; 
       frameLayout.addView(adView, adViewLayoutParams); 

       } 
      } 
     }); 


     this.setContentView(frameLayout, frameLayoutLayoutParams); 

    } 

    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException 
    { 
     SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback); 
    } 

    public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException 
    { 
     mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback() 
     { 
      public void onTimePassed(final TimerHandler pTimerHandler) 
      { 
       mEngine.unregisterUpdateHandler(pTimerHandler); 
       SceneManager.getInstance().createMenuScene(); 

      } 
     })); 
     pOnPopulateSceneCallback.onPopulateSceneFinished(); 
    } 
    @Override 
    protected void onPause() { 
     SceneManager.getInstance().gameScene.onBackKeyPressed(); 
     super.onPause(); 
    } 
    @Override 
    protected void onDestroy() 
    { 
     super.onDestroy(); 
     System.exit(0); 
    } 
} 

GAMEOVER方法,其中我试图把间质性

private void gameover() 
{this.gameovercheck = 1; 
this.setIgnoreUpdate(true); 
Sprite gameover = new Sprite(camera.getCenterX(), camera.getCenterY(), resourcesManager.gameover, vbom); 
this.attachChild(gameover); 
rocket.setCurrentTileIndex(2); 


     activity.runOnUiThread(new Runnable(){ 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 

      InterstitialAd interstitial = new InterstitialAd(activity); 
      interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); 

      AdRequest adRequest = new AdRequest.Builder().build(); 


      interstitial.loadAd(adRequest); 

       interstitial.show(); 



     } 

    }); 
soundOnBtn1 = new Sprite(camera.getCenterX(),camera.getCenterY()-200, resourcesManager.retry, vbom) 
{ 

    public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y) 
    {if(pSceneTouchEvent.isActionDown()) 

     SceneManager.getInstance().loadGameScene1(engine); 
    gameovercheck=0; 
    editor.commit(); 
    editor.commit(); 
    return true; 
    }; 
}; 
this.attachChild(soundOnBtn1); 
this.registerTouchArea(soundOnBtn1); 

回答

0

你必须确保插页式广告预装,否则它不会显示。

预加载它interstitial.loadAd(adRequest);例如就在你开始你的场景时。

而且在isLoaded您GAMEOVER方法检查这样

boolean loaded = interstitialAd.isLoaded(); 
if (loaded) { 
    interstitial.show(); 
} else { 
//loadyourINterstitial 
} 
+0

正在尝试解决,不知道! –

+0

非常感谢!基本上广告花费了时间,并且由于仅尝试一次,所以加载没有发生。现在它的工作。我使用APPFIZZ在Playstore上的游戏中使用了代码。非常感谢 ! –