Hi,
I have integrated Admob in my Cocos2d game and it works fine sometime and some time it gives me follwoing error.
[CCTexture2D requestFreshAd]: unrecognized selector sent to instance.
When i check in debugger it fails on following lines
- (void)refreshAd:(NSTimer *)timer {
[adMobAd requestFreshAd];
}
I don't understand reason behind it. I googled it lot but couldn't find it so finally thought if you can help me on this. Looks like I am missing something minor.
Actually. I have on Scene (Game Scene) and i have added Admob logic in Game Scene. Now the thing is Game Scene is just place holder actual game logic is in Game Layer and also Menu Layer for game over. So, I believe this might be issue as i am calling admob in game scene instead of game layer. I am still not sure about it . Here is my full source code of Game Scene. I wish you can help me.
Header File
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "DataManager.h"
#import "AdMobView.h"
#import "AdMobDelegateProtocol.h"
@interface GameScene : CCScene {
AdMobView* adMobAd;
NSTimer* refreshTimer; // timer to get fresh ads
}
-(id) init;
@end
Implementation File
#import "GameScene.h"
#import "cocos2d.h"
@implementation GameScene
- (void) dealloc
{
[super dealloc];
}
-(id) init {
self = [super init];
if (self != nil) {
dataManager = [DataManager sharedManager];
dataManager.misses = 1;
FallingLayer* layer = [[FallingLayer alloc]init];
self.fallingLayer = layer;
[layer release];
fallingLayer.delegate = self;
[self addChild:fallingLayer];
}
return self;
}
- (void) onEndGame
{
if(fallingLayer != nil)
[self removeChild:fallingLayer cleanup:NO];
if(menuLayer == nil)
{
//MenuLayer *menu = [[MenuLayer alloc] init];
self.menuLayer = [MenuLayer node];
//[menu release];
menuLayer.delegate = self;
}
[self addChild:menuLayer];
}
- (void) restartGame
{
if(menuLayer != nil) [self removeChild:menuLayer cleanup:NO];
[fallingLayer startGame];
}
-(void)onPause {
if(fallingLayer.isPaused == TRUE) {
[fallingLayer deactivateTimers];
}
else {
[fallingLayer activateTimers];
[fallingLayer onResumeGame];
}
}
-(void)onResume {
[fallingLayer activateTimers];
[fallingLayer onResumeGame];
}
-(void) onOptions {
}
-(void) onMainMenu {
[[CCDirector sharedDirector] replaceScene:[HomeScene node]];
}
- (void)didReceiveAd:(AdMobView *)adView {
adMobAd.frame = CGRectMake(0, 0, 320, 35);
[[[CCDirector sharedDirector] openGLView] addSubview:adMobAd];
[refreshTimer invalidate];
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(refreshAd:) userInfo:nil repeats:YES];
}
- (void)onEnter {
adMobAd = [AdMobView requestAdWithDelegate:self];
[adMobAd retain];
[super onEnter];
}
- (void)onExit {
[adMobAd removeFromSuperview];
[adMobAd release];
[super onExit];
}
// Request a new ad. If a new ad is successfully loaded, it will be animated into location.
- (void)refreshAd:(NSTimer *)timer {
[adMobAd requestFreshAd]; // It fails on this part
}
// AdMobDelegate methods
- (NSString *)publisherId {
return @"a14b7310e88dbca"; // this is your publisher ID
}
- (UIColor *)adBackgroundColor {
return [UIColor colorWithRed:0 green:0.749 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
- (UIColor *)primaryTextColor {
return [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
- (UIColor *)secondaryTextColor {
return [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
- (BOOL)mayAskForLocation {
return NO; // this should be prefilled; if not, see AdMobProtocolDelegate.h for instructions
}
@end
I would really appreciate if you can help me on this. I am really frustrated about not finding solution.
Thanks in Advance.
Sunflower