一.安装
下载:http://www.cocos2d-iphone.org/download
进入目录,执行
./install-templates.sh -u
如果之前已经存在可以这样强制更新
./install-templates.sh -f -u
这会安装三个模版:
标准cocos2d模版
带box2d的模版
带chipmunk的模版
二.sprite小例
1.创建一个标准模版生成的工程.
2.在xxxLayer.m中全局声明(也可以不声明,而在后面用getChildByTag)
CCSprite *seeker1; CCSprite *cocosGuy;
3.在init的if块里面重写
// create and initialize our seeker sprite, and add it to this layer seeker1 = [CCSprite spriteWithFile: @"seeker.png"]; seeker1.position = ccp( 50, 100 ); [self addChild:seeker1]; // do the same for our cocos2d guy, reusing the app icon as its image cocosGuy = [CCSprite spriteWithFile: @"Icon.png"]; cocosGuy.position = ccp( 200, 300 ); [self addChild:cocosGuy];
4.动起来(使用action)
上一步的if块最后添加
[self schedule:@selector(nextFrame:)];
然后外面定义nextFrame函数
- (void) nextFrame:(ccTime)dt { seeker1.position = ccp( seeker1.position.x + 100*dt, seeker1.position.y ); if (seeker1.position.x > 480+32) { seeker1.position = ccp( -32, seeker1.position.y ); } }
5.可交互
添加头
#import "CCTouchDispatcher.h"
重写函数
-(void) registerWithTouchDispatcher { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; }
3中if块最后添加
self.isTouchEnabled = YES;
添加函数
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { return YES; } - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint location = [self convertTouchToNodeSpace: touch]; [cocosGuy stopAllActions]; [cocosGuy runAction: [CCMoveTo actionWithDuration:1 position:location]]; }
三.场景切换
开始:
[[CCDirector sharedDirector] runWithScene: [HelloWorld scene]];
替换:
[[CCDirector sharedDirector] replaceScene: [SomeOtherScene scene]];
动态替换(pop时不可动画):
CCDirector sharedDirector] replaceScene: [CCTransitionFade transitionWithDuration:0.5f scene:[SomeOtherScene scene]]];
在你用replace方式替换场景的时候,cocos2d自动清理内存。它会移除所有的节点,停止所有的动作,并且停止所有选择器的预定。所以没
有调用cocos2d的removeAll方法。
另:触摸事件对比
Standard Touch Delegate(标准)
@protocol CCStandardTouchDelegate <NSObject> @optional - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; - (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; @end
Targeted Touch Delegate(定向)
@protocol CCTargetedTouchDelegate <NSObject> - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event; @optional // touch updates: - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event; - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event; - (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event; @end
注意:
1)都需要self.isTouchEnabled = YES;
2)参数一个是touches一个是touch
3)began的返回值,定向的控制事件是否传递
4)注册
-(void) registerWithTouchDispatcher { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; }
不是CCLayer和它的子类要或许点击事件,需要实现协议,并在onEnter中注册触摸种类,并在onExit中卸载
参考:http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:index
http://www.cocos2d-iphone.org/wiki/doku.php/tips:touchdelegates