// When you import this file, you import all the cocos2d classes #import "cocos2d.h" // HelloWorldLayer @interface HelloWorldLayer : CCLayer { CCLabelTTF *score_label; } + (HelloWorldLayer *)instance; // returns a CCScene that contains the HelloWorldLayer as the only child +(CCScene *) scene; @end
m
// on "init" you need to initialize your instance -(id) init { if( (self=[super init])) { //得点ラベルを設置 score_label = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d",0] fontName:@"Marker Felt" fontSize:32]; CGSize size = [[CCDirector sharedDirector] winSize]; score_label.position = ccp(size.width-20, size.height-20); [self addChild:score_label]; //初期点数を入れる NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; [ud setInteger:0 forKey:@"score"]; [ud synchronize]; //タッチイベントをオンに。 self.isTouchEnabled = YES; } return self; } //タッチされると呼び出される -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; NSNumber *score = [ud objectForKey:@"score"]; int ten = [score intValue]; ten++; [ud setInteger:ten forKey:@"score"]; [ud synchronize]; [score_label setString:[NSString stringWithFormat:@"%d",ten]]; }
これで画面を連打するごとに 1点ずつ加算されていく。