gpt4 book ai didi

iphone - cocos2d-iOS - 手势识别器

转载 作者:行者123 更新时间:2023-12-03 18:31:24 29 4
gpt4 key购买 nike

有人设法在 cocos-2d 中实现手势识别吗?

我在这里读过一篇文章,声称已经实现了这一目标,在这里:http://www.cocos2d-iphone.org/forum/topic/8929

我从这里的 git hub 打了补丁: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 :( ");
}

我的点击事件从未被调用。

有人成功了吗?手动进行手势识别以进行滑动检测有多困难?

最佳答案

您需要将手势识别器附加到“链上游”的东西上。不要将它们附加到各个节点;将它们附加到 UIView(即 [[CCDirector sharedDirector] openGLView])。

这就是我所做的:

- (UIPanGestureRecognizer *)watchForPan:(SEL)selector number:(int)tapsRequired {
UIPanGestureRecognizer *recognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:selector] autorelease];
recognizer.minimumNumberOfTouches = tapsRequired;
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:recognizer];
return recognizer;
}

- (void)unwatch:(UIGestureRecognizer *)gr {
[[[CCDirector sharedDirector] openGLView] removeGestureRecognizer:gr];
}

此特定代码用于场景 Controller 的父类(super class)中,因此选择器的目标被硬编码为“self”,但您可以轻松地将其抽象为传入的对象。此外,您还可以推断上述内容,轻松创建轻击、捏合等手势识别器。

在 Controller 的子类中,我只需这样做:

- (MyController *)init {
if ((self = [super init])) {
[self watchForPan:@selector(panning:) number:1];
}
return self;
}

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

CGPoint p;
CGPoint v;

switch( recognizer.state ) {
case UIGestureRecognizerStatePossible:
case UIGestureRecognizerStateBegan:
p = [recognizer locationInView:[CCDirector sharedDirector].openGLView];
(do something when the pan begins)
break;
case UIGestureRecognizerStateChanged:
p = [recognizer locationInView:[CCDirector sharedDirector].openGLView];
(do something while the pan is in progress)
break;
case UIGestureRecognizerStateFailed:
break;
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled:
(do something when the pan ends)
(the below gets the velocity; good for letting player "fling" things)
v = [recognizer velocityInView:[CCDirector sharedDirector].openGLView];
break;
}

}

关于iphone - cocos2d-iOS - 手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4985917/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com