Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t在我的iphone游戏应用程序中以编程方式加载视图中的admob广告_Iphone_Objective C_Ios_Admob - Fatal编程技术网

Can';t在我的iphone游戏应用程序中以编程方式加载视图中的admob广告

Can';t在我的iphone游戏应用程序中以编程方式加载视图中的admob广告,iphone,objective-c,ios,admob,Iphone,Objective C,Ios,Admob,我在objective c中有一个iPhone游戏应用程序,它使用一个私有类以编程方式加载每个视图。(不使用nib文件)我试图在视图中放置admob广告,但似乎无法使其工作。我试图将admob ads视图放在init方法中,但它仍然无法加载视图 以下是我如何初始化数据以加载视图的一些代码: -(id)init { if( (self=[super init])) { played_ = NO; KWSprite* background = [KW

我在objective c中有一个iPhone游戏应用程序,它使用一个私有类以编程方式加载每个视图。(不使用nib文件)我试图在视图中放置admob广告,但似乎无法使其工作。我试图将admob ads视图放在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;
}

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

在较高的层次上,您必须将GadBanerView添加到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;

}