2016-06-11 150 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上的遊戲中使用了代碼。非常感謝 ! –

相關問題