Cocos2D for iPhone应用开发学习点滴是本文要介绍的内容,讲解了Cocos2D的操作,不多说,我们来看内容。
除了 Layer 可以接受触摸事件, 在Cocos2D 0.8以后加入一个新的特性,从而让所有的对象都可以接受触摸事件. 发现大家都不怎么用这个方法,这儿简单介绍一下.
首先添加事件接收者:
[[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:NO];
- 1.
//self为接收者, 优先级参数从0开始 数字越小优先级越高,就会越先接收到事件, ***一个参数表示是否阻止此次事件冒泡
然后实现3个方法:
#pragma mark TouchDispatcherDelegate
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
//你的代码
return YES; //这儿如果返回NO 此次触摸将被忽略
}
- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
//你的代码
}
- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
//你的代码
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
这样,就可以像处理UIView里的事件一样去处理coco2d了.
Edit: 别忘了删除监听者, 要不然......
[[TouchDispatcher sharedDispatcher] removeDelegate:self];
- 1.
小结:Cocos2D for iPhone应用开发学习点滴的内容介绍完了,希望本文对你有所帮助!