前段时间粗略看完《Learn cocos2D Game Development with iOS 5》,作者之一是kobold2d(http://www.kobold2d.com/,很棒的一个开源框架,封装了cocos2d,提供众多的模版,省去大量配置的时间,所有东西都在一个盒子里面,想用什么拿出来就可以使用,非常强大和易用)开源框架的作者Steffen,这本书每一章用一个小游戏来介绍cocos2d的特性,涉及到cocos2d的方方面面,但是章节之间相对独立。看过之后,感觉是知道有这些特性,以后要用到的话再看看也可以拿过来使用,但是对一个完整游戏还是缺乏清晰的认识。
现在打算开始看《Learning Cocos2D》,书其中一个作者是ray wenderlich(http://www.raywenderlich.com/,里面的ios游戏和应用开发教程超一流)。相对而言,这本书介绍了如何使用cocos2d开发一个完整的游戏的过程。这样的话,透过这本书,一方面可以复习一下cocos2d,另一方面可以学习开发一个完整cocos2d游戏。
现在打算把一些关键的地方和一些想法记录下来,一方面可以放慢学习的速度腾出时间来思考练习,另一方面方便以后的翻查。翻滚吧,第一章的记录:
1 CCDirector控制场景切换,它是整个游戏的导演,这只是它其中的一个职责。
2 Cocos2D层次结构,很清晰
ControlsLayer支持触摸事件,点击屏幕,在ControlsLayer的ccTouchEnded里面,
a)获取MainLayer,CCNode *layer = [[CCDirectorsharedDirector].runningScenegetChildByTag:MainLayerTag];
b)然后获取MainLayer的label,CCNode *label = [layergetChildByTag:MainLayerLabelTag];
c)最后[label runAction:sequence];
这样就实现了在ControlsLayer里面控制MainLayer的Sprite。
代码如下,
#ifndef CCHelloWorld_Constants_h #define CCHelloWorld_Constants_h #define MainLayerLabelTag 0 enum { BackgroundLayerTag, MainLayerTag, ControlsLayerTag, TagMax }layerTag; #endif #import <Foundation/Foundation.h> #import "cocos2d.h" @interface BackgroundLayer : CCLayer { } @end #import "BackgroundLayer.h" @implementation BackgroundLayer - (id)init { if (self = [super init]) { CCLabelTTF *label = [CCLabelTTF labelWithString:@"I am background layer" fontName:@"Marker Felt" fontSize:96]; CGSize winSize = [[CCDirector sharedDirector] winSize]; label.position = ccp(winSize.width/2, winSize.height/2); label.color = ccWHITE; [self addChild:label]; } return self; } @end #import <Foundation/Foundation.h> #import "cocos2d.h" @interface MainLayer : CCLayer { } @end #import "MainLayer.h" #import "Constants.h" @implementation MainLayer - (id)init { CCLOG(@"%@ init", NSStringFromClass([self class])); if (self = [super init]) { CCLabelTTF *label = [CCLabelTTF labelWithString:@"I am main layer" fontName:@"Marker Felt" fontSize:64]; CGSize winSize = [[CCDirector sharedDirector] winSize]; label.position = ccp(winSize.width/2, winSize.height/2); label.color = ccGREEN; [self addChild:label z:0 tag:MainLayerLabelTag]; } return self; } @end #import <Foundation/Foundation.h> #import "cocos2d.h" @interface ControlsLayer : CCLayer { } @end #import "ControlsLayer.h" #import "Constants.h" @implementation ControlsLayer - (id)init { if (self = [super init]) { self.isTouchEnabled = YES; CCLabelTTF *label = [CCLabelTTF labelWithString:@"I am controls layer" fontName:@"Marker Felt" fontSize:32]; CGSize winSize = [[CCDirector sharedDirector] winSize]; label.position = ccp(winSize.width/2, winSize.height/2); label.color = ccBLACK; [self addChild:label]; } return self; } - (void)registerWithTouchDispatcher { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; } - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { return YES; } - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { CCLOG(@"%@ ccTouchEnded", NSStringFromClass([self class])); CCNode *layer = [[CCDirector sharedDirector].runningScene getChildByTag:MainLayerTag]; CCNode *label = [layer getChildByTag:MainLayerLabelTag]; CGSize winSize = [[CCDirector sharedDirector] winSize]; CCMoveTo *moveTo = [CCMoveTo actionWithDuration:1.0f position:ccp(50, 50)]; CCMoveTo *moveBack = [CCMoveTo actionWithDuration:1.0f position:ccp(winSize.width/2,winSize.height/2)]; CCSequence *sequence = [CCSequence actions:moveTo, moveBack, nil]; [label runAction:sequence]; [label runAction:[CCFadeOut actionWithDuration:4.0f]]; } @end #import <Foundation/Foundation.h> @interface Level_1 : NSObject + (id)scene; @end #import "Level_1.h" #import "BackgroundLayer.h" #import "MainLayer.h" #import "ControlsLayer.h" #import "Constants.h" @implementation Level_1 + (id)scene { CCLOG(@"%@ scene", NSStringFromClass([self class])); CCScene *scene = [CCScene node]; MainLayer *mainLayer = [MainLayer node]; BackgroundLayer *backgroundLayer = [BackgroundLayer node]; ControlsLayer *controlsLayer = [ControlsLayer node]; [scene addChild:backgroundLayer z:-1 tag:BackgroundLayerTag]; [scene addChild:mainLayer z:0 tag:MainLayerTag]; [scene addChild:controlsLayer z:1 tag:ControlsLayerTag]; return scene; } @end
3 more than ‘Hello World’
a)触摸炸开‘Hello World’,以字母为单位分裂
使用CCLabelBMFont,每一个字符都可以像CCSprite那么操作,旋转、缩放、着色等,参考cocos2d-iphone里面的测试项目LabelTest类Atlas4的实现
#import "cocos2d.h" // HelloWorldLayer @interface HelloWorldLayer : CCLayer { } +(CCScene *) scene; @end // Import the interfaces #import "HelloWorldLayer.h" // HelloWorldLayer implementation @implementation HelloWorldLayer +(CCScene *) scene { CCLOG(@"%@ scene", NSStringFromClass([self class])); CCScene *scene = [CCScene node]; HelloWorldLayer *layer = [HelloWorldLayer node]; [scene addChild: layer]; return scene; } #define kTagLabelBMFont 1 -(id) init { CCLOG(@"%@ init", NSStringFromClass([self class])); if( (self=[super init])) { self.isTouchEnabled = YES; CGSize winSize = [[CCDirector sharedDirector] winSize]; CCLabelBMFont *labelBMFont = [CCLabelBMFont labelWithString:@"Hello World" fntFile:@"bitmapFontTest.fnt"]; labelBMFont.position = ccp(winSize.width/2, winSize.height/2); [self addChild:labelBMFont z:0 tag:kTagLabelBMFont]; } return self; } - (void)registerWithTouchDispatcher { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; } - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { return YES; } - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { // CCLOG(@"%@ ccTouchEnded", NSStringFromClass([self class])); [self explosion:[self getChildByTag:kTagLabelBMFont]]; } - (void)explosion:(CCNode *)label { CGPoint labelPosition = label.position; // CCLOG(@"labelPosition = %@", NSStringFromCGPoint(labelPosition)); for (int i=0; i<[(CCLabelBMFont *)label string].length; i++) { CCSprite *sprite = (CCSprite *)[label getChildByTag:i]; CGPoint point = CGPointZero; CGFloat xRand = CCRANDOM_MINUS1_1() > 0 ? - labelPosition.x : 480 - labelPosition.x; CGFloat yRand = CCRANDOM_MINUS1_1() > 0 ? - labelPosition.y : 320 - labelPosition.y; point = ccp(CCRANDOM_0_1() * xRand, CCRANDOM_0_1() * yRand); // CCLOG(@"randPoint = %@", NSStringFromCGPoint(ccp(xRand, yRand))); CCLOG(@"point = %@", NSStringFromCGPoint(point)); id rot = [CCRotateBy actionWithDuration:.5 angle:arc4random() % 360]; CCMoveTo *moveTo = [CCMoveTo actionWithDuration:.5 position:point]; id spawn = [CCSpawn actions:rot, moveTo, nil]; [sprite runAction:spawn]; } } - (void) dealloc { CCLOG(@"%@ dealloc", NSStringFromClass([self class])); [super dealloc]; } @end
b)使用CCAction模拟自由落体 ;碰到边界弹回,散落在地