【问题标题】:how to add gesture recognizer to ccsprite in cocos2d iphone?如何在 cocos2d iphone 中向 ccsprite 添加手势识别器?
【发布时间】:2012-10-19 11:25:54
【问题描述】:

我是 Cocos2D 的初学者。我想在特定的 CCSprite 上添加手势,但我在 CCLayer 上有手势。我的要求是在 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]]];
    }

}

【问题讨论】:

  • 您不会将手势识别器添加到精灵,您总是将它们添加到 cocos2d 视图(CCDirector 的视图/openGLView 属性)。

标签: iphone cocos2d-iphone


【解决方案1】:

将您的手势识别器添加到图层中,并在您的手势识别器实现中使用 CGRectIntersectsRect 选择器检查您是否触摸了精灵。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    相关资源
    最近更新 更多