一.继承关系
CCNode
CCLayer CCScene CCSprite
CCMenu CCTransitionScene CCLabel
CCLayer
CCLayerColor CCLayerMultiplex CCMenu
CCLayerGrident
一般Layer中都有一个生成包含此layer的场景(scene)的类方法 :
#import "cocos2d.h" // HelloWorldLayer @interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate> { } // returns a CCScene that contains the HelloWorldLayer as the only child +(CCScene *) scene; @end
.m一般有一个init方法
+(CCScene *) scene { // 'scene' is an autorelease object. CCScene *scene = [CCScene node];//生成场景对象 // 'layer' is an autorelease object. HelloWorldLayer *layer = [HelloWorldLayer node];//生成层对象 // add layer as a child to scene [scene addChild: layer];//将层加入场景中 // return the scene return scene;//返回场景对象 } // 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 CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello" fontName:@"Marker Felt" fontSize:10]; //加入语句 label.tag = 13; self.isTouchEnabled = YES; // 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 ); // add the label as a child to this Layer [self addChild: label]; CCLabelTTF *labelTest = [CCLabelTTF labelWithString:@"test" fontName:@"Marker Felt" fontSize:64]; labelTest.tag = 100; labelTest.position = ccp(size.width /2,size.height/4); labelTest.visible = NO; [self addChild:labelTest]; // // Leaderboards and Achievements // // Default font size will be 28 points. [CCMenuItemFont setFontSize:28]; // Achievement Menu Item using blocks CCMenuItem *itemAchievement = [CCMenuItemFont itemWithString:@"Achievements" block:^(id sender) { GKAchievementViewController *achivementViewController = [[GKAchievementViewController alloc] init]; achivementViewController.achievementDelegate = self; AppController *app = (AppController*) [[UIApplication sharedApplication] delegate]; [[app navController] presentModalViewController:achivementViewController animated:YES]; [achivementViewController release]; } ]; // 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, itemLeaderboard, nil]; [menu alignItemsHorizontallyWithPadding:20]; [menu setPosition:ccp( size.width/2, size.height/2 - 50)]; // Add the menu to the layer [self addChild:menu]; //[self scheduleUpdate]; //[self schedule:@selector(update:) interval:1.0f]; } return self; }
1.[self schedule:@selector(update:) interval:1.0f];是cocos2d中常用的定时更新的方法,如果用
[self scheduleUpdate];则它会默认隔一段时间执行名为update:的方法
2.设置响应触摸事件:self.isTouchEnabled = YES;
然后可以实现-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event以及move,end方法
3.触摸事件分发
-(void) registerWithTouchDispatcher { [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES]; } -(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { return YES;//return YES则吞没触摸事件 }
层(CCLayer)是cocos2d中传递触摸信息的载体,系统会将接收到的触摸事件传递给层的对象,默认情况下激活场景包含的所有层对象都会收到该信息,但也有一个特殊的层是例外,那就是——菜单(CCMenu)。
CCMenu其实也继承自CCLayer,因此他们都可以加收到触摸信息,但不同的是,CCMenu会“吞掉”自己接受到的触摸事件,使该信息无法传递给它后面的层,这也很好理解,假如有若干个层叠在一起的按钮,那么当我们点击的时候,响应的肯定是最上边那个,如果所有按钮都响应,那用户就要头大了……菜单就是根据这一特性而从层中被单独抽象出来的,它使得UI中同时只会有一个控件被选中。
那么层又是如何接收到玩家的触摸事件的呢?它和IOS系统又是怎样交互的?答案都在CCTouchDispatcher这个类中。
CCTouchDispatcher是个单例,程序刚启动时,导演类会将其设为EAGLView触摸代理,当玩家有触摸操作时,系统会自动调用它的对应方法。之前在介绍层的时候提到过,CCLayer通过registerWithTouchDispatcher方法将自己注册到CCTouchDispatcher单例中,CCTouchDispatcher检测到触摸信息后,会在注册队列中遍历这些对象,调用它们的响应方法,使它们可以响应触摸事件。
+ (CCTouchDispatcher*)sharedDispatcher
获取单例对象,单例类的惯用方法。
(void) registerWithTouchDispatcher
和CCLayer中的作用一样,只是CCMenu调用的是addTargetedDelegate方法,可以吞掉触摸事件,优先级为-128。
4.触摸点转化为cocos2d中对应的坐标
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView: [touch view]];
CGPoint p = [[CCDirector sharedDirector] convertToGL:touchLocation];