繁体   English   中英

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

[英]cocos2d 3.x how to handle touches outside a sprite

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

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

我建议手动创建一个公共方法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];

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM