Skip to content

Instantly share code, notes, and snippets.

@ricardoquesada
Created December 11, 2012 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricardoquesada/4261879 to your computer and use it in GitHub Desktop.
Save ricardoquesada/4261879 to your computer and use it in GitHub Desktop.
Using blocks without leaks
@implementation HelloWorldLayer
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
// create and initialize a Label
float currSysVer = [[[UIDevice currentDevice] systemVersion]floatValue];
NSString *textLabel = @"Run in iOS 5.x Simulator!!!";
if (currSysVer > 4.0)
textLabel = @"BUG - Tap Play for Info";
CCLabelTTF *label = [CCLabelTTF labelWithString:textLabel fontName:@"Marker Felt" fontSize:24];
// ask director for the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp( size.width /2 , size.height/2 + 50 );
// add the label as a child to this Layer
[self addChild: label];
//
// Leaderboards and Achievements
//
// Default font size will be 28 points.
[CCMenuItemFont setFontSize:28];
// Achievement Menu Item using blocks
__block id obj = self;
CCMenuItem *itemAchievement = [CCMenuItemFont itemWithString:@"Game Center" block:^(id sender) {
GKAchievementViewController *achievementViewController = [[GKAchievementViewController alloc] init];
achievementViewController.achievementDelegate = obj;
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] presentModalViewController:achievementViewController animated:YES];
[achievementViewController release];
}
];
// itemAchievement.releaseBlockAtCleanup = YES;
/*
// Leaderboard Menu Item using blocks
CCMenuItem *itemLeaderboard = [CCMenuItemFont itemWithString:@"Leaderboard" block:^(id sender) {
GKLeaderboardViewController *leaderboardViewController = [[GKLeaderboardViewController alloc] init];
leaderboardViewController.leaderboardDelegate = self;
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] presentModalViewController:leaderboardViewController animated:YES];
[leaderboardViewController release];
}
];
*/
CCMenu *menu = [CCMenu menuWithItems:itemAchievement, nil];
[menu alignItemsHorizontallyWithPadding:20];
[menu setPosition:ccp( size.width/2, size.height/2 - 20)];
// Add the menu to the layer
[self addChild:menu];
// Menu Item using blocks
// __block id obj = self;
CCMenuItem *gameItem = [CCMenuItemFont itemWithString:@"Play" block:^(id sender) {
[obj playNotice];
//[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[GameLayer scene] ]];
}
];
// gameItem.releaseBlockAtCleanup = YES;
CCMenu *menu2 = [CCMenu menuWithItems:gameItem, nil];
[menu2 alignItemsHorizontallyWithPadding:20];
[menu2 setPosition:ccp( size.width/2, size.height/2 - 60)];
[self addChild:menu2];
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
int newCount = [app launchCounter];
CCLabelTTF *counter = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Runs: %i",newCount] fontName:@"Marker Felt" fontSize:24];
counter.position = ccp( size.width -60 , 20 );
[self addChild: counter];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(gotNotification:)
name:@"GetReady"
object:nil];
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment