杂记

cocos2d中,最好不要使用view,应该在CCDirectory身上的view是在最上层的

添加进去后就不容易移除。

cocos2d里面,触摸事件,一定要开启协议,cctouchonebyonedelete,或者开启多点触摸,settouch,

ios中,还有一个简单的触摸控制,只要添加一次手指就可以UIPanGestureRecognizer

片段程序

 

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {

 

if (recognizer.state == UIGestureRecognizerStateBegan) {

 

CGPoint touchLocation = [recognizer locationInView:recognizer.view]; touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation]; touchLocation = [self convertToNodeSpace:touchLocation]; [self selectSpriteForTouch:touchLocation];

 

} elseif (recognizer.state == UIGestureRecognizerStateChanged) {

 

CGPoint translation = [recognizer translationInView:recognizer.view]; translation = ccp(translation.x, -translation.y); [self panForTranslation:translation]; [recognizer setTranslation:CGPointZero inView:recognizer.view];

 

} elseif (recognizer.state == UIGestureRecognizerStateEnded) {

 

if (!selSprite) { float scrollDuration =0.2; CGPoint velocity = [recognizer velocityInView:recognizer.view]; CGPoint newPos = ccpAdd(self.position, ccpMult(velocity, scrollDuration)); newPos = [self boundLayerPos:newPos];

 

[self stopAllActions]; CCMoveTo *moveTo = [CCMoveTo actionWithDuration:scrollDuration position:newPos]; [self runAction:[CCEaseOut actionWithAction:moveTo rate:1]]; }

 

} }

 

cocos2d中,播放音乐

其实加入音乐和音效的代码很简单,那么就先给出来:

#import "SimpleAudioEngine.h"
......
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"bg.caf"];
[[SimpleAudioEngine sharedEngine] preloadEffect:@"click.caf"];

[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"bg.caf" loop:YES];
[[SimpleAudioEngine sharedEngine] playEffect:@"click.caf"];

解释一下,首先导入 SimpleAudioEngine.h, 如果需要的话,可以预先导入,就是头两个 preload..的方法,防止要放的时候临时加载引起延迟。

后两个是用来播放音效的。 播放背景音乐有个参数 loop,可以设置是否循环播放。

至于这个.caf是怎么转换的,可以用下面的命令:

afconvert -f caff -d LEI16@44100 -c 1 in.wav out.caf

这行命令是打在terminal里的,用cd命令先进入到存放音效的目录中,然后使用上面的命令转换文件格式,然后导入到项目中,就可以使用了。

因为这个命令是内建的不需要多余的安装步骤。

 

posted @ 2013-09-05 23:09  悟性思远  阅读(184)  评论(0编辑  收藏  举报