cocos2d 3.x如何处理精灵外的触摸

时间:2014-06-21 05:32:47

标签: ios iphone cocos2d-iphone

我需要处理精灵之外的触摸。即当我在精灵外面触摸时,仍然需要调用精灵的touchBegan方法。我该怎么做?

请注意,我使用的是3.x版本,具有优先级的CCTargetedTouchDelegate不再存在。

1 个答案:

答案 0 :(得分:1)

我建议手动创建一个公共方法ccTouchBegan,它返回是否吞下了触摸(就像cocos2d 2.0一样)

在你的画布中:

- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    BOOL swallowed = NO;

    for (id child in _childNodes) {
        if ([child ccTouchBegan:touch event:event]) {
            swallowed = YES;
        }
    }

    if (swallowed) {
        return;
    }

    //continue the canvas touch handler

}

并在精灵中:

- (BOOL)ccTouchBegan:(UITouch *)touch event:(UIEvent *)event {

    //same usage as cocos2d 2.x

}


//and in the touch callback
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {

    if ([self ccTouchBegan:touch event:event]) {
        //swallow

    }
    else {
        //do not swallow, pass the touch
        [super touchBegan:touch withEvent:event];

    }
}