0

我正在 iPhone 上开发 cocos2d 游戏。游戏完美运行。
现在我想在我的游戏菜单中添加一个 admob 广告。我可以看到广告,但是玩了几秒钟(或几分钟)后,游戏崩溃了(没有错误消息......)
你能看出问题出在哪里吗?

菜单.h

@interface Menu : CCColorLayer <AdMobDelegate> {  
    AdMobView *adMobAd;  
    NSTimer *refreshTimer;  
} 

菜单.m

+(id) scene {...}  
-(id) init {...}  
-(void) dealloc {...}

- (void)didReceiveAd:(AdMobView *)adView {  
    adMobAd.frame = CGRectMake(-260, 432, 320, 48);  
    CGAffineTransform makeLandscape = CGAffineTransformMakeRotation(M_PI * 0.5f);  
    makeLandscape = CGAffineTransformTranslate(makeLandscape, -216, -134);  
    adMobAd.transform = makeLandscape;  
    [[[CCDirector sharedDirector] openGLView] addSubview:adMobAd];  
    [refreshTimer invalidate];  
    refreshTimer = [NSTimer scheduledTimerWithTimeInterval:AD_REFRESH_PERIOD target:self selector:@selector(refreshAd:) userInfo:nil repeats:YES];  
}  

- (UIViewController *)currentViewController {
    return nil;
}

- (void)onEnter {
    adMobAd = [AdMobView requestAdWithDelegate:self];
    [adMobAd retain];
    [super onEnter];
}

- (void)onExit {
    [adMobAd removeFromSuperview];
    [adMobAd release];
    [super onExit];
}

- (void)refreshAd:(NSTimer *)timer {
    [adMobAd requestFreshAd];
}

- (NSString *)publisherId {
    return @"zzzzzzzzzzzzzz";
}

- (UIColor *)adBackgroundColor {
    return [UIColor colorWithRed:0.2 green:0.6 blue:1 alpha:1];
}

- (UIColor *)primaryTextColor {
    return [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
}

- (UIColor *)secondaryTextColor {
    return [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
}

- (BOOL)mayAskForLocation {
    return NO;
}

谢谢!:-)

4

1 回答 1

1

我建议您摆脱NSTimer并使用 aCCTimer代替(并使用 安排它CCScheduler)。

您可以使用以下代码执行此操作:

    refreshTimer = [CCTimer timerWithTarget:self
                                   selector:@selector(refreshAd:)
                                   interval:AD_REFRESH_PERIOD];
    [[CCScheduler sharedScheduler] scheduleTimer:refreshTimer];
于 2010-05-07T20:08:13.117 回答