2014-06-14 4 views
1

안녕하세요 각 게임 종료 후 광고를 표시하고 싶지만 일부 문제가 있습니다. 누군가 AdMob 삽입 광고를 구현할 수 있도록 도와 주실 수 있습니까? 전체 소스 코드에는 활동을 확장하는 하나의 클래스 만 있습니다. 앱에서 AdMob 삽입 광고를 구현하는 방법을 알고 있지만 이전에는 게임에서 사용하지 않았습니다. 코드를 어디에 추가해야하며 코드는 무엇입니까? 고맙습니다.게임 종료 후 내 게임에서 AdMob 삽입 광고를 표시하려고합니다.

내 GameOverscreen 코드입니다.

public class GameOverScene extends CCLayer { 
     //fields 
     private SoundManager _soundMananger; 
     private CCSprite _background; 

     private CCMenu _menu; 
     private CCMenu _restart; 

     private CCLabel _yourScore; 
     private CCLabel _bestScore; 
     //methods 
     public static CCScene scene() { 
      CCScene scene = CCScene.node(); 
      GameOverScene layer = new GameOverScene();  
      scene.addChild(layer); 
      return scene; 
     } 

     public GameOverScene() { 
      super(); 
      BeetleshotMainActivity.app.setHideAdView(false); 
      _soundMananger = SoundManager.sharedSoundResourceManager(); 
      CGSize size = CCDirector.sharedDirector().winSize(); 
      float scaleX = size.width/768.0f; 
      float scaleY = size.height/1024.0f; 

      if (BeetleshotMainActivity.app._soundplay == true) { 
       _soundMananger.bStopMenuSound(); 
       _soundMananger.bPlayMenuSound(); 
      } 

      _background = CCSprite.sprite("Image/Gameover and Clear/Over_Bg.png"); 
      _background.setScaleX(scaleX); 
      _background.setScaleY(scaleY); 
      _background.setPosition(CGPoint.ccp(size.width/2, size.height/2)); 
      this.addChild(_background); 

      CCMenuItemImage menuItem = CCMenuItemImage.item("Image/Gameover and Clear/MaiinNenu_n.png", "Image/Gameover and Clear/MaiinNenu_d.png", this, "menuAction"); 
      menuItem.setScaleX(scaleX); 
      menuItem.setScaleY(scaleY); 
      _menu = CCMenu.menu(menuItem); 
      _menu.setPosition(CGPoint.ccp(size.width*1.0f/2.0f, size.height*3.0f/10.0f)); 
      this.addChild(_menu); 

      menuItem.setPosition(CGPoint.ccp(menuItem.getPosition().x-50.0f, menuItem.getPosition().y)); 
      CCMoveBy dropTitle = CCMoveBy.action(1.5f, CGPoint.ccp(50.0f, 0.0f)); 
      CCEaseBounceOut easeDrop = CCEaseBounceOut.action(dropTitle); 
      menuItem.runAction(easeDrop); 

      CCMenuItemImage restartItem = CCMenuItemImage.item("Image/Gameover and Clear/Restart_n.png", "Image/Gameover and Clear/Restart_d.png", this, "restartAction"); 
      restartItem.setScaleX(scaleX); 
      restartItem.setScaleY(scaleY); 
      _restart = CCMenu.menu(restartItem); 
      _restart.setPosition(CGPoint.ccp(size.width/2.0f, (float) (size.height*4.2f/10.0f))); 
      this.addChild(_restart); 

      restartItem.setPosition(CGPoint.ccp(restartItem.getPosition().x+50.0f, restartItem.getPosition().y)); 
      dropTitle = CCMoveBy.action(1.5f, CGPoint.ccp(-50.0f, 0.0f)); 
      easeDrop = CCEaseBounceOut.action(dropTitle); 
      restartItem.runAction(easeDrop); 


    //  CCMenuItemImage shareI = CCMenuItemImage.item("Image/MainMenu/share.png", "Image/MainMenu/Share.png", this, "Share"); 
      fb.setScaleX(scaleX); 
      fb.setScaleY(scaleY); 
      _restart = CCMenu.menu(shareI); 
      _restart.setPosition(CGPoint.ccp(size.width/2.0f, (float) (size.height*1.5f/10.0f))); 
      this.addChild(_restart); 

      shareI.setPosition(CGPoint.ccp(restartItem.getPosition().x+50.0f, restartItem.getPosition().y)); 
      dropTitle = CCMoveBy.action(1.5f, CGPoint.ccp(-160.0f, 0.0f)); 
      easeDrop = CCEaseBounceOut.action(dropTitle); 
      shareI.runAction(easeDrop); 

I wan to handle the click.. How do i Do that? I want to show createChooser dialog when user press on the share icon. 

      Intent iShare = new Intent(Intent.ACTION_SEND); 
     iShare.setType("text/plain"); 
     iShare.putExtra(Intent.EXTRA_SUBJECT, "Download The Game"); 
     iShare.putExtra(Intent.EXTRA_TEXT, "Get The Game"+" "+"https://play.google.com/store/apps/details?id=com.jdtfans.jdtfootballkickers"); 
     startActivity(Intent.createChooser(iShare,"Share Via")); 


Above code i want to use when user press share icon. How do i do that? 


      float scaleFactor = CCDirector.sharedDirector().winSize().height/1024 * 2.3f; 

      _yourScore = CCLabel.makeLabel(String.format("%d", BeetleshotMainActivity.app._score), CGSize.make(120.0f*scaleFactor, 60.0f*scaleFactor), TextAlignment.CENTER, "Font/Imagica.ttf", 24.0f*scaleFactor);_yourScore.setPosition(CGPoint.ccp(size.width*1.32f/2.0f, (float) (size.height*1.12/2.0f))); 

      this.addChild(_yourScore); 
      _yourScore.setColor(ccColor3B.ccBLUE); 

      // add this 
      _bestScore = CCLabel.makeLabel(String.format("%d", GameScene._hScoreValue), CGSize.make(120.0f*scaleFactor, 60.0f*scaleFactor), TextAlignment.CENTER, "Font/Imagica.ttf", 24.0f*scaleFactor);_bestScore.setPosition(CGPoint.ccp(size.width*1.32f/2.0f, (float) (size.height*1/2.0f))); 

      this.addChild(_bestScore); 
      _bestScore.setColor(ccColor3B.ccBLUE); 
      // 
      switch(BeetleshotMainActivity.app._locationLevel) { 
       case 1: 
        BeetleshotMainActivity.app._level = 1; 
        break; 
       case 2: 
        BeetleshotMainActivity.app._level = 2; 
        break; 
       case 3: 
        BeetleshotMainActivity.app._level = 3; 
        break; 
       default: 
        break; 
      } 

      int currentScore = BeetleshotMainActivity.app._score; 
      BeetleshotMainActivity.app._score = 0;  

      if (BeetleshotMainActivity.app._location == 1) { 
       switch(BeetleshotMainActivity.app._locationLevel) { 
        case 1: 
         BeetleshotMainActivity.app.loadScore("score1"); 
         break; 
        case 2: 
         BeetleshotMainActivity.app.loadScore("score2"); 
         break; 
        case 3: 
         BeetleshotMainActivity.app.loadScore("score3"); 
         break; 
        default: 
         break; 
       } 
      } 

      if (currentScore > BeetleshotMainActivity.app._score) { 
       BeetleshotMainActivity.app._score = currentScore; 

       if (BeetleshotMainActivity.app._location == 1) { 
        switch(BeetleshotMainActivity.app._locationLevel) { 
         case 1: 
          BeetleshotMainActivity.app.saveScore("score1"); 
          break; 
         case 2: 
          BeetleshotMainActivity.app.saveScore("score2"); 
          break; 
         case 3: 
          BeetleshotMainActivity.app.saveScore("score3"); 
          break; 
         default: 
          break; 
        } 
       } 
      } 
     } 

     public void menuAction(Object sender) { 
      if (BeetleshotMainActivity.app._soundplay == true) 
       _soundMananger.ePlayButtonClickSound();  
      CCDirector.sharedDirector().replaceScene(CCFadeTransition.transition(0.7f, MenuScene.scene())); 
     } 

     public void restartAction(Object sender) { 
      if (BeetleshotMainActivity.app._soundplay == true) 
       _soundMananger.ePlayButtonClickSound();  
      CCDirector.sharedDirector().replaceScene(CCFadeTransition.transition(0.7f, GameScene.scene())); 
     } 

    } 




This is my only activity class that i have in my project. 


     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      app = this; 
      mContext = this; 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      ccMacros.CCLOG("BeetleShot Game", "OnCreate"); 

      RelativeLayout layout = new RelativeLayout(this); 
      layout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 

      mGLSurfaceView = new CCGLSurfaceView(this); 

      // Create the adView 
      adView = new AdView(this, AdSize.BANNER, ADMOB_PUBLISH_ID); 

      // Initiate a generic request to load it with an ad 
      adView.loadAd(new AdRequest()); 
      adView.setAdListener(this); 
      // Add the adView to it 
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT); 
      params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
      params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
      adView.setLayoutParams(params); 

      layout.addView(mGLSurfaceView); 
      layout.addView(adView); 
      //// 


      // create the interstitial 
      interstitial = new InterstitialAd(this, "**********"); 

      // load interstitialAd 
      Ads.loadInterstitialAd(interstitial); 

      // set Ad Listener to use the callbacks below 
      interstitial.setAdListener(this); 
      setContentView(layout); 

      CCDirector.sharedDirector().attachInView(mGLSurfaceView); 
      CCDirector.sharedDirector().setDisplayFPS(false); 
      CCDirector.sharedDirector().setAnimationInterval(1.0f/30); 

      CCTexture2D.setDefaultAlphaPixelFormat(Config.ARGB_8888); 

      _soundplay = true; 
      _playerSound = true; 
      _controlShow = false; 
      _playerPos = 1; 
      _level = 1; 
      _firstBonus = 0; 
      _secondBonus = 0; 
      _thirdBonus = 0; 
      _locationLevel = 1; 

      CCDirector.sharedDirector().runWithScene(LogoScene.scene()); 
     } 

     @Override 
     protected void onStart() { 
      super.onStart(); 
     } 

     @Override 
     protected void onResume() { 
      super.onResume();  
      CCDirector.sharedDirector().onResume(); 
     } 

     @Override 
     protected void onPause() { 
      super.onPause(); 
      SoundManager.sharedSoundResourceManager()._soundEngine.pauseSound(); 
      CCDirector.sharedDirector().onPause(); 
     } 

     @Override 
     protected void onDestroy() { 
      super.onDestroy();  
      CCDirector.sharedDirector().end(); 
     SoundManager.releaseSoundResourceManager(); 
     } 

     public void loadScore (String filename) { 
      try { 
       ccMacros.CCLOG("file reading", "reading scores from " + filename); 
       FileInputStream fIn = openFileInput(filename); 
       InputStreamReader isr = new InputStreamReader(fIn); 
       char[] buf = new char[6]; 
       isr.read(buf); 
       _score = Integer.parseInt(new String(buf).trim()); 
       isr.close(); 
       fIn.close(); 
       ccMacros.CCLOG("file reading result", "" + _score); 
      } catch (Exception e) { 
      } 
     } 

     public void saveScore (String filename) { 
      try { 
       ccMacros.CCLOG("file writing", "writing scores to " + filename); 
       FileOutputStream fOut = openFileOutput(filename, MODE_WORLD_READABLE); 
       OutputStreamWriter osw = new OutputStreamWriter(fOut); 
       osw.write("" + _score); 
       osw.flush(); 
       osw.close(); 
       fOut.close(); 
      } catch (Exception e) { 
      } 
     } 

     public CGPoint getBufPlayerPos() { 
      return _m_playerPos; 
     } 

     public void setBufPlayerPos (CGPoint rpos) { 
      _m_playerPos = rpos; 
     } 

     // Admob Setting 


     public void onDismissScreen(Ad arg0) { 
      // TODO Auto-generated method stub 

     } 

     public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) { 
      // TODO Auto-generated method stub 

     } 

     public void onLeaveApplication(Ad arg0) { 
      // TODO Auto-generated method stub 

     } 

     public void onPresentScreen(Ad arg0) { 
      // TODO Auto-generated method stub 

     } 

     public void onReceiveAd(Ad arg0) { 
      // TODO Auto-generated method stub 
      //Log.d(TAG, "+++++++++++++ New admob has received! ++++++++++++++"); 
     } 


     Handler handler = new Handler() { 
      public void handleMessage(Message msg) { 
       if(msg.what == 0) { 
        adView.setVisibility(View.INVISIBLE); 
       } 
       else { 
        adView.setVisibility(View.VISIBLE);   
       } 
      } 
     }; 

     public void setHideAdView(boolean bHide) { 
      if(bHide) { 
       handler.sendEmptyMessage(0); 
      } 
      else { 
       handler.sendEmptyMessage(1);    
      } 
     } 

    } 
+0

상위 클래스 "CCLayer"가 활동 중입니까? –

+0

아니요 ... 해당 활동 없음 –

+0

인터페이스를 정의하여 광고를 호출하는 리스너 객체를 사용할 수 있습니다. 귀하의 질문에 잘 대답 할 수 있도록 기본 활동을 게시하십시오. Thanks –

답변

1

일반적으로, 외관 디자인 패턴이를 달성하기 위해 사용되지만 이미 BeetleshotMainActivity.app를 통해 장면 클래스의 활동의 컨텍스트에 액세스 할 수 있기 때문에, 당신이 사용할 수있는 다음과 그 다음에

을 주요 활동 클래스 : 삽입 광고를 표시 할 메소드를 정의합니다. 당신은 당신이 setAdView() 메소드했던 것처럼 핸들러를 사용하지만 runOnUiThread를 사용하여 간단하게 많이 할 수 있습니다 다음과 같이

public void showInterstitialAd() { 
runOnUiThread(new Runnable(){ 
     @Override 
     public void run() { 
      if (interstitial != null && interstitial.isReady()) 
      interstitial.show(); 
     }   
    }); 

}

그런 다음 생성자를 통해 게임에 바로 삽입 광고를 보여줍니다

public GameOverScene() { 
     super(); 
     BeetleshotMainActivity.app.setHideAdView(false); 
     BeetleshotMainActivity.app.showInterstitialAd(); 
      ----- 
      ------ 
} 

onDismissScreen 콜백 메서드는 사용자가 광고를 클릭 한 후 액티비티로 돌아올 때 호출됩니다. 따라서 광고가 호출되면 광고를 다시로드하여 후속 게임 오버시 광고가 표시됩니다.

public void onDismissScreen(Ad arg0) { 
     // TODO Auto-generated method stub 

// create the interstitial 
     interstitial = new InterstitialAd(this, "***********"); 

     // load interstitialAd 
     Ads.loadInterstitialAd(interstitial); 

     // set Ad Listener to use the callbacks below 
     interstitial.setAdListener(this); 
    } 
+0

새로운 runnable() {} 유형의 메소드 run()은 수퍼 클래스 메소드를 대체해야합니다 ...이 오류가 발생합니다 ... –

+0

고맙습니다. 지금 일하고 있습니다 ... –

+0

다행스럽게도 도움이 될 수 있습니다.) –