我需要处理精灵之外的触摸。即当我在精灵外面触摸时,仍然需要调用精灵的touchBegan方法。我该怎么做?
请注意,我使用的是3.x版本,具有优先级的CCTargetedTouchDelegate不再存在。
答案 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];
}
}