一直很忙没工夫仔细的研究下2d。终于有时间来做自己喜欢的事情了。好吧,现在手里有了安装包,知易的教程和源码,还有一本中文教程。
装好了库,xcode4.4 + cocos2d1.0.1版本。虽说已经有2.0了,但是都差不多就这个吧。省着再去下载了,公司网络也不怎么好。
原库里头有实例。
这里的文章不会是教程,不可能全部讲解。只是记下我在学习时自己感到有些需要研究的地方,就说明下,给自己个深刻的记忆。
第一个比较迷糊的地方就是锚点和positon了,一开始没反应过来。
默认anchorpoint是(0.5,0.5),为你所设图形或者精灵的中心c的位置。
如果你设置为(0.3,0.5),图中红点的位置,那么他的position的点的位置是按照anchorpoint来设置的。比方说A点为anchorpoint点,是(0,0),这时候你设置posistion为(5,5)的时候,矩形是以(5,5)点为矩形的左下角点。整个2d的坐标系是基于opengl的,opengl的学习也是不可少的,不过为了快速的开发,现在不深究这些原理了。记住坐标系是左手坐标系。现在主要根据知易教程来开始:helloword,花十分钟熟悉下就好了。今天主要分析游戏主菜单。
SysMenu
SysMenu.m文件
1 #import "SysMenu.h"
2 #import "cocos2d.h"
3
4 #import "GameLayer.h"
5 #import "GameCntrolMenu.h"
6 #import "SettingsLayer.h"
7
8 @implementation SysMenu
9
10 +(id)scene
11 {
12 CCScene * scence = [CCScene node];
13 SysMenu * layer = [SysMenu node];
14 [scence addChild:layer];
15 return scence;
16 }
17
18 -(id)init
19 {
20 self = [super init];
21 if (self) {
22 CCSprite * sp = [CCSprite spriteWithFile:@"bg.png"];
23 sp.anchorPoint = CGPointZero;
24
25 [self addChild:sp z:0 tag:1];
26
27
28 //设置菜单
29 [CCMenuItemFont setFontSize:25];
30
31 CCMenuItem * newGame = [CCMenuItemFont itemFromString:@"新游戏"
32 target:self
33 selector:@selector(newGame:)];
34 [CCMenuItemFont setFontSize:25];
35
36 CCMenuItem * loadGame = [CCMenuItemFont itemFromString:@"继续游戏"
37 target:self
38 selector:@selector(continueGame:)];
39
40 CCMenuItem * GameSettings = [CCMenuItemFont itemFromString:@"游戏设置"
41 target:self
42 selector:@selector(OnSettings:)];
43
44 CCMenuItem * helpGame = [CCMenuItemFont itemFromString:@"帮助"
45 target:self
46 selector:nil];
47
48 CCMenuItem * quitGame = [CCMenuItemFont itemFromString:@"退出"
49 target:self
50 selector:@selector(onQuit:)];
51
52 //把菜单选项加到菜单上
53 CCMenu * mn = [CCMenu menuWithItems:newGame,loadGame,GameSettings,helpGame,quitGame, nil];
54 [mn alignItemsVertically];
55
56 [self addChild:mn z:1 tag:2];
57 }
58 return self;
59 }
60
61 -(void)newGame:(id)sender
62 {
63 CCScene * sc = [CCScene node];
64 [sc addChild:[GameLayer node]];
65 [sc addChild:[GameCntrolMenu node]];
66
67 [[CCDirector sharedDirector] replaceScene:[CCTransitionShrinkGrow transitionWithDuration:1.2f scene:sc]];
68 }
69
70 -(void)continueGame:(id)sender
71 {
72
73 }
74
75 -(void)OnSettings:(id)sender
76 {
77 CCScene * sc = [CCScene node];
78 [sc addChild:[SettingsLayer node]];
79
80 [[CCDirector sharedDirector] replaceScene:[CCTransitionJumpZoom transitionWithDuration:1.2f scene:sc]];
81 }
82
83 -(void)onQuit:(id)sender
84 {
85
86 }
87
88 -(void)dealloc
89 {
90 [super dealloc];
91 }
92
93 @end
我很郁闷的一件事就是,教程已经很老了,用的是0.99版本的2d,和1.0.1有很多的api不同。好吧,它打不倒我们。说不定2.0也会变api,咱们有强大的度神。
切换场景的时候,有很多过度动画,想要点炫的场景切换效果就仔细研究下CCTransition.m吧。
这里我随意挑了一个效果。 CCTransitionJumpZoom
CCScene * sc = [CCScene node];
[sc addChild:[SettingsLayer node]];
[[CCDirector sharedDirector] replaceScene:[CCTransitionJumpZoom transitionWithDuration:1.2f scene:sc]];
你会发现,层的切换其实很容易,初始化scence,把要显示的layer加到scence上就可以了。谁想显示就replacescence下。
不用考虑内存的问题,因为他已经帮你管理了,你要相信autorelease的实力。
好了咱们有菜单了,就开始游戏主页是显示:
GameLayer
#import "GameLayer.h"
#import "SysMenu.h"
@implementation GameLayer
- (id) init
{
self = [super init];
if (self)
{
CCSprite *sp = [CCSprite spriteWithFile:@"Space.png"];
sp.anchorPoint = CGPointZero;
sp.opacity = 100;
[self addChild:sp z:0 tag:1];
// Add Status Items
CCLabelBMFont *lbScore = [CCLabelBMFont labelWithString:@"Time: 0" fntFile:@"font09.fnt"];
lbScore.anchorPoint = ccp(1.0, 1.0);
lbScore.scale = 0.6;
[self addChild:lbScore z:1 tag:3];
lbScore.position = ccp(310, 450);
CCSpriteBatchNode * mgr = [CCSpriteBatchNode batchNodeWithFile:@"flight.png" capacity:5];
[self addChild:mgr z:0 tag:4];
CCSprite *sprite = [CCSprite spriteWithTexture:mgr.texture rect:CGRectMake(0,0,31,30) ];
[mgr addChild:sprite z:1 tag:5];
sprite.scale = 1.1;
sprite.anchorPoint = ccp(0, 1);
sprite.position = ccp(10, 460);
CCLabelBMFont *lbLife = [CCLabelBMFont labelWithString:@"3" fntFile:@"font09.fnt"];
lbLife.anchorPoint = ccp(0.0, 1.0);
lbLife.scale = 0.6;
[self addChild:lbLife z:1 tag:6];
lbLife.position = ccp(50, 450);
// change score
[self schedule:@selector(step:) interval:1];
// Add flight
flight = [CCSprite spriteWithTexture:mgr.texture rect:CGRectMake(0,0,31,30)];
flight.position = ccp(160, 30);
flight.scale = 1.6;
[mgr addChild:flight z:1 tag:99];
CCSpriteFrame * frame = [CCSpriteFrame frameWithTexture:mgr.texture rect:CGRectMake(0, 0, 31, 30)];
NSArray * array = [[NSArray alloc] initWithObjects:frame, nil];
CCAnimation * animation = [CCAnimation animationWithFrames:array delay:0.2];
for (int i = 0; i < 3; i++) {
int x= i%3;
[animation addFrameWithTexture:mgr.texture rect:CGRectMake(x*32, 0, 31, 30)];
}
// CCAnimation *animation = [CCAnimation animationWithName:@"flight" delay:0.2f];
// for(int i=0;i<3;i++) {
// int x= i % 3;
// [animation addFrameWithTexture:mgr.texture rect:CGRectMake(x*32, 0, 31, 30) ];
// }
id action = [CCAnimate actionWithAnimation: animation];
[flight runAction:[CCRepeatForever actionWithAction:action]];
// accept touch now!
self.isTouchEnabled = YES;
}
return self;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
[flight runAction: [CCMoveTo actionWithDuration:1 position:ccp(convertedLocation.x, convertedLocation.y)]];
return;
}
-(void) step:(ccTime) dt
{
time += dt;
NSString *string = [NSString stringWithFormat:@"Time: %d", (int)time];
CCLabelAtlas *label1 = (CCLabelAtlas*) [self getChildByTag:3];
[label1 setString:string];
}
- (void) dealloc
{
[super dealloc];
}
@end
可以看到注释的部分是老版的代码,新版的已经不再这么写了。(我怎么感觉老版的比新版的省很多事呢,可能后面兼容c++吧)
有了游戏界面和主菜单了,当然不能缺少的是设置界面。
SettingsLayer
1 #import "SettingsLayer.h"
2 #import "cocos2d.h"
3 #import "SysMenu.h"
4
5 @implementation SettingsLayer
6
7 -(id)init
8 {
9 [super init];
10
11 //set label font
12 [CCMenuItemFont setFontName:@"American Typewriter"];
13 [CCMenuItemFont setFontSize:18.0];
14
15 CCMenuItemFont * title1 = [CCMenuItemFont itemFromString:@"音效"];
16 [title1 setIsEnabled:NO];
17 [CCMenuItemFont setFontName:@"Marker Felt"];
18 [CCMenuItemFont setFontSize:26];
19 CCMenuItemToggle * item1 = [CCMenuItemToggle itemWithTarget:self
20 selector:@selector(menuCallback:) items:
21 [CCMenuItemFont itemFromString:@"开"],
22 [CCMenuItemFont itemFromString:@"关"],
23 nil];
24
25 [CCMenuItemFont setFontName:@"American Typewriter"];
26 [CCMenuItemFont setFontSize:18];
27 CCMenuItemFont * title2 = [CCMenuItemFont itemFromString:@"音乐"];
28 [title2 setIsEnabled:NO];
29 [CCMenuItemFont setFontName:@"Marker Felt"];
30 [CCMenuItemFont setFontSize:26];
31 CCMenuItemToggle * item2 = [CCMenuItemToggle itemWithTarget:self
32 selector:@selector(menuCallback:)
33 items:
34 [CCMenuItemFont itemFromString:@"开"],
35 [CCMenuItemFont itemFromString:@"关"],nil];
36
37 [CCMenuItemFont setFontName: @"American Typewriter"];
38 [CCMenuItemFont setFontSize:18];
39 CCMenuItemFont * title3 = [CCMenuItemFont itemFromString:@"AI设置"];
40 [title3 setIsEnabled:NO];
41 [CCMenuItemFont setFontName:@"Marker Felt"];
42 [CCMenuItemFont setFontSize:26];
43 CCMenuItemToggle * item3 = [CCMenuItemToggle itemWithTarget:self
44 selector:@selector(menuCallback:)
45 items:
46 [CCMenuItemFont itemFromString:@"攻击型"],
47 [CCMenuItemFont itemFromString:@"保守型"],nil];
48
49 [CCMenuItemFont setFontName:@"American Typewriter"];
50 [CCMenuItemFont setFontSize:18];
51 CCMenuItemFont * title4 = [CCMenuItemFont itemFromString:@"难度"];
52 [title4 setIsEnabled:NO];
53 [CCMenuItemFont setFontName:@"Marker Felt"];
54 [CCMenuItemFont setFontSize:26];
55 CCMenuItemToggle * item4 = [CCMenuItemToggle itemWithTarget:self
56 selector:@selector(menuCallback:) items:[CCMenuItemFont itemFromString: @"妈妈的宝贝"], nil];
57 NSArray * more_items = [NSArray arrayWithObjects:
58 [CCMenuItemFont itemFromString:@"菜鸟"],
59 [CCMenuItemFont itemFromString:@"高手"],
60 [CCMenuItemFont itemFromString:@"骨灰级"],nil];
61
62 [item4.subItems addObjectsFromArray:more_items];
63
64 item4.selectedIndex = 0;
65
66 [CCMenuItemFont setFontName:@"Marker Felt"];
67 [CCMenuItemFont setFontSize:26];
68
69 CCLabelBMFont * label = [CCLabelBMFont labelWithString:@"Go back" fntFile:@"font01.fnt"];
70 CCMenuItemLabel * back = [CCMenuItemLabel itemWithLabel:label target:self selector:@selector(backCallback:)];
71 back.scale = 0.8;
72
73 CCMenu * menu = [CCMenu menuWithItems:
74 title1,title2,
75 item1,item2,
76 title3,title4,
77 item3,item4,
78 back,nil];
79 [menu alignItemsInColumns:
80 [NSNumber numberWithUnsignedInt:2],
81 [NSNumber numberWithUnsignedInt:2],
82 [NSNumber numberWithUnsignedInt:2],
83 [NSNumber numberWithUnsignedInt:2],
84 [NSNumber numberWithUnsignedInt:1],nil];
85
86 [self addChild:menu];
87
88 return self;
89
90 }
91
92 -(void)dealloc
93 {
94 [super dealloc];
95 }
96
97 -(void) menuCallback: (id) sender
98 {
99 NSLog(@"selected item: %@ index:%d", [sender selectedItem], [sender selectedIndex] );
100 }
101
102 -(void) backCallback: (id) sender
103 {
104 CCScene *sc = [CCScene node];
105 [sc addChild:[SysMenu node]];
106
107 [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInL transitionWithDuration:1.2 scene:sc]];
108
109 }
110
111 @end
原文链接: https://www.cnblogs.com/LoveJiaQi/archive/2012/11/08/cocos2d_learn_1.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/68698
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!