1

2D ゲームに admob を実装しました。このゲームの開発に cocos2d を使用しています。admob バナーの位置を設定するのに誰か助けてくれませんか。縦向きのユニバーサル ゲームを持っています。デバイスの下部にバナーを表示したいのですが、現在、あるビューから別のビューに移動するとバナーが削除され、削除されません。ここに私のコードがあります:

-(void)onEnter
{
    [super onEnter];

     #ifdef ENABLE_ADMOB
   // AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication]delegate];

    viewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] viewController];

    // AppController *app =  (AppController*)[[UIApplication sharedApplication]   delegate];    
    // Create a view of the standard size at the bottom of the screen.
    // Available AdSize constants are explained in GADAdSize.h.


     mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];

    // Specify the ad's "unit identifier." This is your AdMob Publisher ID.



    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        mBannerView.adUnitID =@"a15062384653c9e"; 
    } 
    else
    {
        mBannerView.adUnitID =@"a15062392a0aa0a"; 
    }


    // Let the runtime know which UIViewController to restore after taking
    // the user wherever the ad goes and add it to the view hierarchy.


    //size

    mBannerView.rootViewController = viewController;
    [viewController.view addSubview:mBannerView];

    // Initiate a generic request to load it with an ad.
    [mBannerView loadRequest:[GADRequest request]];

     CGRect frame = mBannerView.frame;

    frame.origin.y = (viewController.view.bounds.size.height) ;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        CGSize AdSize = kGADAdSizeLeaderboard.size;

        frame.origin.y = 950; 
    } 
    else
    {
        CGSize AdSize = kGADAdSizeBanner.size;

        frame.origin.y = 430;
    }

    mBannerView.frame = frame;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    frame = mBannerView.frame;
    frame.origin.y = 430;

    mBannerView.frame = frame;
    [UIView commitAnimations];    

    #endif
}

-(void)showBannerView
 {
    if (mBannerView) 
    {
        [UIView animateWithDuration:0.5
                          delay:0.1
                        options: UIViewAnimationCurveEaseOut
                     animations:^
     {
         CGRect frame = mBannerView.frame;
         frame.origin.x = 0;
         mBannerView.frame = frame;
     } 
                     completion:^(BOOL finished)
     {
     }];
}

}

  -(void)hideBannerView
  {
      if (mBannerView) 
       {
           [UIView animateWithDuration:0.5
                          delay:0.1
                        options: UIViewAnimationCurveEaseOut
                     animations:^
     {
         CGRect frame = mBannerView.frame;
         frame.origin.y = -50.0f;
         mBannerView.frame = frame;
     } 
                     completion:^(BOOL finished)
     {
     }];
}

 }


 -(void)dismissAdView
  {
     #ifdef ENABLE_ADMOB
     if (mBannerView) 
     {
         [UIView animateWithDuration:0.5
                          delay:0.1
                        options: UIViewAnimationCurveEaseOut
                     animations:^
         {
         CGRect frame = mBannerView.frame;
         frame.origin.y = -50.0f;
         mBannerView.frame = frame;
     } 
                     completion:^(BOOL finished)
     {
         [mBannerView setDelegate:nil];
         [mBannerView removeFromSuperview];
         mBannerView = nil;

     }];

  }
 #endif  

 }
4

2 に答える 2

1

これは、iPhone ゲームの Admob バナーの一般的なコードです。

#import "GADBannerView.h"

typedef enum _bannerType
{
    kBanner_Portrait_Top,
    kBanner_Portrait_Bottom,
    kBanner_Landscape_Top,
    kBanner_Landscape_Bottom,
}CocosBannerType;

#define BANNER_TYPE  kBanner_Landscape_Bottom //change this on need basis

@interface MyMainMenu : CCLayer
{
    GADBannerView *mBannerView;
    CocosBannerType mBannerType;
    float on_x, on_y, off_x, off_y;
}

@implementation MyMainMenu


-(void)onEnter
{
    [super onEnter];
    [self createAdmobAds];
}

-(void)onExit 
{
    [self dismissAdView];
    [super onExit];
}

-(void)createAdmobAds
 {
    mBannerType = BANNER_TYPE;

    AppController *app =  (AppController*)[[UIApplication sharedApplication] delegate];
    // Create a view of the standard size at the bottom of the screen.
    // Available AdSize constants are explained in GADAdSize.h.

    if(mBannerType <= kBanner_Portrait_Bottom)
        mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
    else
        mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerLandscape];

    // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
    mBannerView.adUnitID = MY_BANNER_UNIT_ID;

    // Let the runtime know which UIViewController to restore after taking
    // the user wherever the ad goes and add it to the view hierarchy.

    mBannerView.rootViewController = app.navController;
    [app.navController.view addSubview:mBannerView];

    // Initiate a generic request to load it with an ad.
    [mBannerView loadRequest:[GADRequest request]];

    CGSize s = [[CCDirector sharedDirector] winSize];

    CGRect frame = mBannerView.frame;

    off_x = 0.0f;
    on_x = 0.0f;

    switch (mBannerType)
    {
        case kBanner_Portrait_Top:
        {
            off_y = -frame.size.height;
            on_y = 0.0f;
        }
            break;
        case kBanner_Portrait_Bottom:
        {
            off_y = s.height;
            on_y = s.height-frame.size.height;
        }
            break;
        case kBanner_Landscape_Top:
        {
            off_y = -frame.size.height;
            on_y = 0.0f;
        }
            break;
        case kBanner_Landscape_Bottom:
        {
            off_y = s.height;
            on_y = s.height-frame.size.height;
        }
            break;

        default:
            break;
    }

    frame.origin.y = off_y;
    frame.origin.x = off_x;

    mBannerView.frame = frame;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    frame = mBannerView.frame;
    frame.origin.x = on_x;
    frame.origin.y = on_y;


    mBannerView.frame = frame;
    [UIView commitAnimations];
}


-(void)showBannerView
{
    if (mBannerView)
    {
        [UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         {
             CGRect frame = mBannerView.frame;
             frame.origin.y = on_y;
             frame.origin.x = on_x;

             mBannerView.frame = frame;
         }
                         completion:^(BOOL finished)
         {
         }];
    }

}


-(void)hideBannerView
 {
    if (mBannerView)
    {
        [UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         {
             CGRect frame = mBannerView.frame;
             frame.origin.y = off_y;
             frame.origin.x = off_x;
         }
                         completion:^(BOOL finished)
         {
         }];
    }

}

-(void)dismissAdView
 {
    if (mBannerView) 
    {
        [UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         { 
             CGRect frame = mBannerView.frame;
             frame.origin.y = off_y;
             frame.origin.x = off_x;
             mBannerView.frame = frame;
         } 
                         completion:^(BOOL finished)
         {
             [mBannerView setDelegate:nil];
             [mBannerView removeFromSuperview];
             mBannerView = nil;

         }];
    }
}
于 2013-06-16T06:33:14.550 に答える
-1

私は解決策と立場を手に入れました。彼のゲームでAdMobを使用したい場合は、このコードを使用できます。@rajに感謝します。彼は私を大いに助けてくれます。

`

  -(void)onEnter
  {
  [super onEnter];

  #ifdef ENABLE_ADMOB
  // AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication]delegate];

   viewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate]  viewController];

// AppController *app =  (AppController*)[[UIApplication sharedApplication] delegate];    
// Create a view of the standard size at the bottom of the screen.
// Available AdSize constants are explained in GADAdSize.h.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
{
 mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeLeaderboard];
}
else {
    mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
}

// Specify the ad's "unit identifier." This is your AdMob Publisher ID.



if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
{
    mBannerView.adUnitID =@"a15062384653c9e"; 
} 
else
{
    mBannerView.adUnitID =@"a15062392a0aa0a"; 
}


// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.


//size



mBannerView.rootViewController = viewController;
[viewController.view addSubview:mBannerView];


// Initiate a generic request to load it with an ad.
[mBannerView loadRequest:[GADRequest request]];



   CGRect frame = mBannerView.frame;

//  frame.origin.y = (viewController.view.bounds.size.height) ;



if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
{

   frame = mBannerView.frame;
    frame.origin.y = 950; 
    frame.origin.x = 20;
} 
else
{

   frame = mBannerView.frame;
    frame.origin.y = 430;
    frame.origin.x = 0;
}






 mBannerView.frame = frame;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];



mBannerView.frame = frame;
[UIView commitAnimations];    

#endif


 }


 -(void)showBannerView
  {
   if (mBannerView) 
  {
    [UIView animateWithDuration:0.5
                          delay:0.1
                        options: UIViewAnimationCurveEaseOut
                     animations:^
     {
         CGRect frame = mBannerView.frame;
         frame.origin.x = 0;
         mBannerView.frame = frame;
     } 
                     completion:^(BOOL finished)
     {
     }];
   }

 }


 -(void)hideBannerView
{
  if (mBannerView) 
   {
    [UIView animateWithDuration:0.5
                          delay:0.1
                        options: UIViewAnimationCurveEaseOut
                     animations:^
     {
         CGRect frame = mBannerView.frame;
         frame.origin.y = -50.0f;
         mBannerView.frame = frame;
     } 
                     completion:^(BOOL finished)
     {
     }];
  }

 }


 -(void)dismissAdView
{
 #ifdef ENABLE_ADMOB
if (mBannerView) 
{
    [UIView animateWithDuration:0.5
                          delay:0.1
                        options: UIViewAnimationCurveEaseOut
                     animations:^
     {
         CGRect frame = mBannerView.frame;
         frame.origin.y = -50.0f;
         mBannerView.frame = frame;
     } 
                     completion:^(BOOL finished)
     {
         [mBannerView setDelegate:nil];
         [mBannerView removeFromSuperview];
         mBannerView = nil;

     }];

 }
  #endif  

}

`

于 2012-10-03T22:20:50.163 に答える