我正在iPhone上制作cocos2d游戏。游戏工作完美。
现在我想在我的游戏菜单中添加一个admob广告。我可以看到广告,但在播放几秒钟(或几分钟)后,游戏崩溃(没有错误消息......)
你能看出问题出在哪里吗?
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;
}
谢谢! : - )
答案 0 :(得分:1)
我建议您删除NSTimer
并使用CCTimer
代替(并使用CCScheduler
进行安排)。
您可以使用以下代码执行此操作:
refreshTimer = [CCTimer timerWithTarget:self
selector:@selector(refreshAd:)
interval:AD_REFRESH_PERIOD];
[[CCScheduler sharedScheduler] scheduleTimer:refreshTimer];