1

我在目标 c 中有一个 iPhone 游戏应用程序,它使用私有类以编程方式加载每个视图。(不使用 nib 文件)我试图在视图中放置一个 admob 广告,但似乎我无法让它工作。我试图将我的 admob 广告视图放在 init 方法中,但它仍然没有加载视图。

以下是我如何初始化数据以加载视图的一些代码:

-(id)init
{
    if( (self=[super init]))
    {
        played_ = NO;

        KWSprite* background = [KWSprite spriteWithFile:@"title_background.png"];
        background.position = ccp(winSize_.width/2, winSize_.height/2);

        KWSprite* logo = [KWSprite spriteWithFile:@"logo.png"];
        logo.position = ccp(winSize_.width/2, 260);    

        CCMenuItemImage* start = [CCMenuItemImage itemFromNormalImage:@"start.png" selectedImage:@"start_selected.png" target:self selector:@selector(pressStartButton:)]
        CCMenuItemImage* credit  = [CCMenuItemImage itemFromNormalImage:@"credit.png" selectedImage:@"credit_selected.png" target:self selector:@selector(pressCreditButton:)];
        CCMenuItemImage* iADButton  = [CCMenuItemImage itemFromNormalImage:@"noAdsD.png" selectedImage:@"noAdsD@2x.png" target:self selector:@selector(pressiAdButton:)];

        CCMenuItemImage* howto = [CCMenuItemImage itemFromNormalImage:@"howto.png" selectedImage:@"howto_selected.png" target:self selector:@selector(pressHowtoButton:)];

        CCMenu* menu = [CCMenu menuWithItems:howto, start, credit, iADButton, nil];
        [menu alignItemsHorizontally];
        menu.position = ccp(winSize_.width/2, 40); 
        [self addChild:background];
        [self addChild:logo];
        [self addChild:menu];
    }
    return self;
}
4

1 回答 1

0

看起来你在这里使用的是 Cocos2D。如果您使用的是 v0.99.5b3 之后的版本,我建议您将 AdMob 代码放在 RootViewController 类中。OpenGL 性能会更好,因为您没有将 UIKit 视图与 OpenGL 视图结合起来。

在高层次上,您必须将 GADBannerView 添加到您的 RootViewController 的视图中,然后减小 Cocos2D OpenGL 视图的大小以确保广告显示。这可能如下所示(此代码假定广告位于顶部):

- (void)showBanner {
  // Frame of the main RootViewController which we call the root view.
  CGRect rootViewFrame = self.view.frame;
  // Frame of the main RootViewController view that holds the Cocos2D view.
  CGRect glViewFrame = [[CCDirector sharedDirector] openGLView].frame;
  // Frame of the GADBannerView
  CGRect bannerViewFrame = bannerView_.frame;
  CGRect frame = bannerViewFrame;
  // The updated x and y coordinates for the origin of the banner.
  CGFloat yLocation = 0.0;
  CGFloat xLocation = 0.0;


  // Move the root view underneath the ad banner.
  glViewFrame.origin.y = bannerViewFrame.size.height;
  // Center the banner using the value of the origin.
  if (UIInterfaceOrientationIsLandscape(toInt)) {
    // The superView has not had its width and height updated yet so use those
    // values for the x and y of the new origin respectively.
    xLocation = (rootViewFrame.size.height -
                    bannerViewFrame.size.width) / 2.0;
  } else {
    xLocation = (rootViewFrame.size.width -
                    bannerViewFrame.size.width) / 2.0;
  }
  frame.origin = CGPointMake(xLocation, yLocation);
  bannerView_.frame = frame;

  if (UIInterfaceOrientationIsLandscape(toInt)) {
    // The super view's frame hasn't been updated so use its width
    // as the height.
    glViewFrame.size.height = rootViewFrame.size.width -
                                    bannerViewFrame.size.height;
    glViewFrame.size.width = rootViewFrame.size.height;
  } else {
    glViewFrame.size.height = rootViewFrame.size.height -
                                    bannerViewFrame.size.height;
  }
  [[CCDirector sharedDirector] openGLView].frame = glViewFrame;

}
于 2012-07-16T18:50:04.673 回答