大家知道如何实现cocos2d中的手势识别吗?我在一篇文章中,看到过类似效果的实现方法:
http://www.cocos2d-iphone.org/forum/topic/8929
在github上找到了相关的补充资料:
https://github.com/xemus/cocos2d-GestureRecognizers/blob/master/README
此外,我创建了一个CCSprite(CCNode的子类别)的子类别:
-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect {
if( (self=[super initWithTexture:texture rect:rect]) )
{
CCGestureRecognizer* recognizer;
recognizer = [CCGestureRecognizer
CCRecognizerWithRecognizerTargetAction:[[[UITapGestureRecognizer alloc]init] autorelease]
target:self
action:@selector(tap:node:)];
[self addGestureRecognizer:recognizer];
}
return self;
}
delegate方法:
- (void) swipe:(UIGestureRecognizer*)recognizer node:(CCNode*)node
{
NSLog(@" I never get called :( ");
}
在这种方法下,我设定的点击事件(tap event)始终不响应。
大家遇到过类似的问题吗?在cocos2d中实现swipe detection的手势识别很困难吗?
答:cc.
你需要将手势识别器(gesture recognizer)添加到"up the chain"中。不要只添加这些代码,而是要将他们和UIView结合在一起(例如:[[CCDirector sharedDirector] openGLView])
我的做法是:
这段特殊的代码是用在超场景控制器中(scene controller),所以选择器的目标是筛选出,可以进行“自我”硬式编码(hard-code)的代码。
对于控制器的子类别,我是这样做的:
答:Krzysztof Zabłocki
如果你不想人为控制一切操作,我建议你可以创建一种简单的类别,用来添加手势识别功能,详情可查看:
http://www.merowing.info/2012/03/using-gesturerecognizers-in-cocos2d/
或者可以在github上找到这种功能的实现源码:https://github.com/krzysztofzablocki/CCNode-SFGestureRecognizers