如何将手势识别器添加到cocos2d iphone中的ccsprite?
问题描述:
我是Cocos2D的初学者。我想在特定的CCSprite上添加手势,但我在CCLayer上获得了手势。我的要求是在CCSprite上添加手势。这个怎么做?如何将手势识别器添加到cocos2d iphone中的ccsprite?
我的代码:
-(id) initWithMask
{
CCSprite *mask=[CCSprite spriteWithFile:@"circle_mask.png"];
[self addChild:moveGlopFront z:1];
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handlePushSceneGestureRecognizer:)];
[self addGestureRecognizer:swipeGestureRecognizer];
swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
swipeGestureRecognizer.delegate = self;
}
-(void)handlePushSceneGestureRecognizer:(UISwipeGestureRecognizer*)aGestureRecognizer
{
for(CCNode *child in layer.children)
{
[child stopAllActions];
}
for(CCNode *child in layer.children)
{
[moveGlopFront setTexture:[moveGlopBack texture]];
[moveGlopFront runAction:[CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:5.0 angle:360]]];
}
}
答
添加您的手势识别的层代替,并用CGRectIntersectsRect选择来检查,如果你摸的精灵,你的手势识别的实现。
将代码添加到您的问题中。 – NANNAV
您不添加手势识别器到精灵,您总是将它们添加到cocos2d视图(CCDirector的视图/ openGLView属性)。 – LearnCocos2D