2010-05-07 88 views
0

我正在研究iPhone上的cocos2d遊戲。遊戲完美運作。
現在我想在我的遊戲菜單中添加一個admob廣告。我可以看到廣告,但在玩了幾秒鐘(或幾分鐘)後,遊戲崩潰(沒有錯誤信息...)
你能看到問題在哪裏嗎?將admob添加到帶有cocos2d的iPhone應用程序會使遊戲崩潰...?

Menu.h

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

Menu.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; 
} 

謝謝! :-)

回答

1

我建議你擺脫NSTimer並使用CCTimer來代替(並使用CCScheduler安排它)。

您可以使用此代碼做到這一點:

refreshTimer = [CCTimer timerWithTarget:self 
            selector:@selector(refreshAd:) 
            interval:AD_REFRESH_PERIOD]; 
    [[CCScheduler sharedScheduler] scheduleTimer:refreshTimer]; 
+0

非常感謝,現在還沒有崩潰! – pimpampoum 2010-05-07 20:47:31

+0

很高興聽到它的工作! – pgb 2010-05-07 20:48:17