I'm working on a cocos2d game on iPhone. The game work perfectly.
Now I want to add an admob ad in the menu of my game. I can see the ad, but after few seconds (or minutes) of playing, the game crash (with no error message...) Can you see where the problem is?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]开发者_如何学Go;
[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;
}
Thanks! :-)
I'd suggest you get rid of the NSTimer
and use a CCTimer
instead (and schedule it using CCScheduler
).
You can do it with this code:
refreshTimer = [CCTimer timerWithTarget:self
selector:@selector(refreshAd:)
interval:AD_REFRESH_PERIOD];
[[CCScheduler sharedScheduler] scheduleTimer:refreshTimer];
精彩评论