Clipping / Masking a CCNode / CCSprite

In order to shave off the parts of a CCNode or a CCSprite, the parts of which you do not want to be shown, you’ll have to overload the visit method of the CCNode or the CCSprite. Like so:

	if([[CCDirector sharedDirector] enableRetinaDisplay:YES])
		//glScissor(2*x, 2*y, 2*width, 2*height);
		//glScissor(x, y, width, height);

Since the OpenGL functions interpret points literally, you can’t just use the local points (in which Cocos2d interprets the points differently for retina and non-retina displays). This is why I have a check against retina displays.

Adwhirl + Cocos2d iPhone iPad implementation tutorial

I read a blogpost over at: Couldn’t get it to work while following the tutorial. There’s also a lot of extra code that I didn’t need (such as offline ad storage, and optional ads for free and paid versions). Anyways, here’s how I implemented it:
1. Follow the readme first, add any extra frameworks as needed for each of your ad networks and add the files into your project.
2. In the AppDelegate.h here’s what mine looks like (imported AdwhirlDelegateProtocol.h, specify that this AppDelegate will follow the AdwhirlDelegate, add the property for root controller, and list out the extra methods needed to implement the AdwhirlDelegate):

#import <UIKit/UIKit.h>
#import "AdWhirlDelegateProtocol.h"
@class RootViewController;

@interface AppDelegate : NSObject <UIApplicationDelegate, AdWhirlDelegate> {
	UIWindow			*window;
	RootViewController	*viewController;
- (NSString *)adWhirlApplicationKey;
- (UIViewController *)viewControllerForPresentingModalView;
- (void)applicationDidFinishLaunching:(UIApplication *)application;
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) RootViewController *viewController;

3. In AppDelegate.m, implemented the extra methods as follows (make sure to import “AdWhirlView.h”):

- (NSString *)adWhirlApplicationKey {
- (UIViewController *)viewControllerForPresentingModalView {
	return viewController;
- (void)adWhirlDidReceiveAd:(AdWhirlView *)adWhirlView { 
    [adWhirlView rotateToOrientation:UIInterfaceOrientationLandscapeRight]; //My game is landscape only, adjust yours accordingly
	CGSize adSize = [adWhirlView actualAdSize];
	CGRect newFrame = adWhirlView.frame;
	newFrame.size = adSize;
	newFrame.origin.x = [[CCDirector sharedDirector] winSize].width/2-adSize.width/2;
	newFrame.origin.y = [[CCDirector sharedDirector] winSize].height-adSize.height;
	adWhirlView.frame = newFrame;

Make sure to input your adwhirlapplicationkey (get it from the adwhirl website).

4. Lastly, add this to the end of applicationDidFinishLaunching in AppDelegate.m

	AdWhirlView *adWhirlView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
	[[[CCDirector sharedDirector] openGLView] addSubview:adWhirlView];

The ads should now be visible.

Converting Objects into NSData for sending over remote connections (Bluetooth/Wifi)

First you need your object to conform to the NSCoding protocol. (Note: PacketType is a simple enumerated type. If you aren’t sure, imagine packetType is an int).

#import <Foundation/Foundation.h>
#import "PacketObject.h"
@interface PacketJoinObject : PacketObject <NSCoding>{
        PacketTypes packetType;
	NSString *name;
@property PacketTypes packetType;
@property (copy) NSString *name;


Here’s the PacketJoinObject.m

#import "PacketJoinObject.h"

@implementation PacketJoinObject
@synthesize packetType, name;
#define kPacketTypes @"PacketTypes"
#define kName @"Name"
	[coder encodeInt:self.packetType forKey:kPacketTypes];
	[coder forKey:kName];
	self.packetType = [aDecoder decodeIntForKey:kPacketTypes]; = [aDecoder decodeObjectForKey:kName];
	return self;
	return name;
	[name release];
	[super dealloc];

Then in your code that will send this object to another iOS device:

PacketJoinObject *newJoinPacket = [[PacketJoinObject alloc] init];
newJoinPacket.packetType = kPacketTypeJoin; = @"SomeName";

Then in your receiver code:

PacketJoinObject *joinPacket = [NSKeyedUnarchiver unarchiveObjectWithData:data];