触摸检测ccnode锚定所以0,0是中心?

时间:2013-09-23 01:48:07

标签: cocos2d-iphone

我试图让ccnodes(精灵,标签等)单独响应触摸事件......

我有以下代码:

@implementation CCNode (TouchDetection)

(BOOL)containsPoint:(CGPoint)point padding:(NSArray *)padding {
    NSAssert(([padding count] == 4), @"padding must consist of [top, right, bottom, left] values!");
    int paddingTop    = [padding[0] integerValue];
    int paddingRight  = [padding[1] integerValue];
    int paddingBottom = [padding[2] integerValue];
    int paddingLeft   = [padding[3] integerValue];
    CGRect rect = CGRectMake(0, 0, paddingLeft + self.contentSize.width + paddingRight, paddingTop + self.contentSize.height + paddingBottom);
    return CGRectContainsPoint(rect, locationInNodeSpace);
}

-(BOOL)containsTouch:(UITouch *)touch padding:(NSArray *)padding {    
    CCDirector* director = [CCDirector sharedDirector];
    CGPoint locationGL = [director convertToGL:[touch locationInView:[[CCDirector sharedDirector] view]]];
    return [self containsPoint:locationGL padding:padding];
}

@end

这种方法很有效,除了在锚定的节点上,以便0,0位于中心。这种实现显然不适用于这种情况,因为它被比作一个原点为0的矩形...所以,当我点击左侧的任何这些节点时,它们不会注册因为locationInNodeSpace是负数,因此在矩形之外。

我将代码更改为:

(BOOL)containsPoint:(CGPoint)point padding:(NSArray *)padding {
    NSAssert(([padding count] == 4), @"padding must consist of [top, right, bottom, left] values!");
    int paddingTop    = [padding[0] integerValue];
    int paddingRight  = [padding[1] integerValue];
    int paddingBottom = [padding[2] integerValue];
    int paddingLeft   = [padding[3] integerValue];
    float paddedWidth = paddingLeft + self.contentSize.width + paddingRight;
    float paddedHeight = paddingTop + self.contentSize.height + paddingBottom;
    CGRect rect = CGRectMake(-paddedWidth / 2, -paddedHeight / 2, paddedWidth, paddedHeight);
    CGPoint locationInNodeSpace = [self convertToNodeSpace:point];
    return CGRectContainsPoint(rect, locationInNodeSpace);
}

并且针对这些情况修复了它,但是现在它对于0,0不在其中心的节点不能正常工作。

我是以错误的方式来做这件事的吗?是否最好将所有内容固定在一起?

更新

显然这个问题与锚点没有关系。我有一个CCNode,其contentSize设置为220,180。它的锚点是0,0 ......我添加了一些NSLogging来看看这里发生了什么。当我点击左下角时,我看到:

2013-09-28 09:24:11.205 [4101:c07] ================> anchor:{0, 0}  location in node:{-77, -65.5}  location in world:{1131, 1298.5}  rect:{{0, 0}, {220, 180}}  boundingBox:{{220, 170}, {220, 180}}  padding: 0 0 0 0

它不会检测到此触摸,因为节点空间中的位置是否定的。 CCNode中心左侧和下方的所有内容都位于负位置。

当我点击右上角时,我看到:

2013-09-28 09:24:12.175 [4101:c07] ================> anchor:{0, 0}  location in node:{89, 64.5}  location in world:{1297, 1428.5}  rect:{{0, 0}, {220, 180}}  boundingBox:{{220, 170}, {220, 180}}  padding: 0 0 0 0

所以,使用CCNode的边界框无论如何都无法帮助我。根据锚点修改矩形无论如何都无法帮助我。

我需要理解为什么我会得到负面价值......任何人都可以对这些废话有所了解吗?

1 个答案:

答案 0 :(得分:0)

要取消锚点的影响,您可以执行以下操作:

CGRect rect = CGRectMake(self.position.x -(self.contentSize.width * self.anchorPoint.x),
                         self.position.y - (self.contentSize.height * self.anchorPoint.y),
                         paddingLeft + self.contentSize.width + paddingRight, 
                         paddingTop + self.contentSize.height + paddingBottom);

但我想知道CGRect rect = [self boundingBox]是否适合您。

让我知道它是否有效!无法测试它。