FlurryAds Interstitial ads with Cocos2d v3

This is my first foray into ad-supported territory. I am really liking Cocos2d and recently my focus has been on the newest v3 (currently rc4). In this tutorial we will put up some interstitial ads for iphone and/or ipad.

Cocos2d v3 has a great new installer. It gives you a couple options in XCode. It’s even compatible with the latest XCode (5.1) and ARC.

-all those frameworks.
AdSupport.framework (Mark as Optional to support < iOS 6.0)
CoreGraphics.framework
Foundation.framework
MediaPlayer.framework
Security.framework
StoreKit.framework (Mark as Optional to support < iOS 6.0)
SystemConfiguration.framework
UIKit.framework

//do the first delegate stuff
In the AppDelegate.h go into the
-(BOOL) application: didFinishLaunchingWithOptions:

[Flurry setCrashReportingEnabled:YES];
[Flurry startSession:@"YOUR_API_KEY"];
[FlurryAds enableTestAds:YES];

In this case we’re going to modify the HelloWorldScene.
Right under the backButton make a button to invoke our interstitial.

CCButton *interstitialButton = [CCButton buttonWithTitle:@"[ Interstitial ]" fontName:@"Verdana-Bold" fontSize:18.0f];
interstitialButton.positionType = CCPositionTypeNormalized;
interstitialButton.position = ccp(0.70f, 0.80f); // Top Right of screen
[interstitialButton setTarget:self selector:@selector(showInterstitial:)];
[self addChild:interstitialButton];</code>

 

We’re also going to prime the pump and run an ad. onEnter is out chance to set the delegate and fetch an ad. We’ll make sure that the first grab is just a preload.

(ADSPACENAME is defined as an NSString.)

-(void)onEnter {
    [super onEnter];
 
    //    rootViewController = [(CCAppDelegate *)[[CCAppDelegate delegate viewController];
    [FlurryAds setAdDelegate:self];
    [FlurryAds fetchAdForSpace:ADSPACENAME frame:CGRectMake((self.contentSize.width/2),(self.contentSize.height/2), self.contentSize.width, self.contentSize.height) size:FULLSCREEN];
 
}

Now we’ll get to the meat/your protein of choice. Our view to give will be [[CCDirector sharedDirector] view]. On iOS CCDirector inherits from CC_VIEWCONTROLLER which is a typedef for UIViewController. When I first saw that I was very “We are all made of stars” moment.

-(void) showInterstitial:(id) sender {
// Check if ad is ready. If so, display the ad
if ([FlurryAds adReadyForSpace:ADSPACENAME]) {
[FlurryAds displayAdForSpace:ADSPACENAME onView:[[CCDirector sharedDirector] view]];
} else {
// Fetch an ad
[FlurryAds fetchAdForSpace:ADSPACENAME frame:CGRectMake((self.contentSize.width/2),(self.contentSize.height/2), self.contentSize.width, self.contentSize.height) size:FULLSCREEN];
}
}

Now we’ll fill in the two delegate methods we want to override. You’ll want to stop whatever audio you’ve got. That’s it. Pause, resume and cleaning up the delegate in onExit:.

- (BOOL) spaceShouldDisplay:(NSString*)adSpace interstitial:(BOOL)
interstitial {
if (interstitial) { //pause state
// [[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];
[[CCDirector sharedDirector] pause];
}
// Continue ad display
return YES;
}
 
- (void)spaceDidDismiss:(NSString *)adSpace interstitial:(BOOL)interstitial {
if (interstitial) { // Resume app state
// [[SimpleAudioEngine sharedEngine] resume];
[[CCDirector sharedDirector] resume];
}
}
 
-(void)onExit {
[super onExit];
[FlurryAds setAdDelegate:nil];
}

p.s. Your ADSPACENAME is an NSString of the moniker you gave for an Adspace on Flurry’s website. You can find the adspace with the tab ‘Publisher’, then left nav ‘Inventory’, then ‘Ad Spaces’ link: http://support.flurry.com/index.php?title=Publisher/GettingStarted.