2

通过帮助和建议,我为我的精灵创建了一条路径,以便只能触摸不透明的部分。这是我想出的路径:

    path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, endTouch.x, endTouch.y);
    CGPathAddLineToPoint(path, NULL, 0, 250);
    CGPathAddLineToPoint(path, NULL, 30, 0);
    CGPathCloseSubpath(path);

这适用于我所有的其他课程,除了一个。无论我在哪里点击,xcode 都会使用以下代码在“外部”打印:

for(int i = 0; i < [sprArray count]; i++)
{
    CCSprite *sprite = (CCSprite *)[sprArray objectAtIndex:i];
    if(CGRectContainsPoint([sprite boundingBox], location))
    {
        selectedSprite = sprite;
        location = [selectedSprite convertToNodeSpace:location];
        if (CGPathContainsPoint(path, NULL, location, NO) ) 
        {
            NSLog(@"inside");
        }
        else 
        {
            NSLog(@"outside");
        }

        break;
    }
}

如果我进入 if 条件,我只能移动我的精灵,否则,即使我点击实际的彩色精灵,它也不会得到我设置的路径。我的测量值有误吗?如果没有,我做错了什么?这类似于我正在尝试使用的图像......

样本

4

1 回答 1

1

这仅适用于图像大小相同的情况。根据图像大小计算坐标。

在此处输入图像描述

CGPathMoveToPoint(path,    NULL,   54, 0 ); //1: 54 = distance from left, 0 = dis fem bottom
CGPathAddLineToPoint(path, NULL,   28, 34 );
CGPathAddLineToPoint(path, NULL,   36, 76 );
CGPathAddLineToPoint(path, NULL,   51, 104 );
CGPathAddLineToPoint(path, NULL,   46, 147 );
CGPathAddLineToPoint(path, NULL,   67, 147 );
CGPathAddLineToPoint(path, NULL,   70, 105 );
CGPathAddLineToPoint(path, NULL,   56, 66 );
CGPathAddLineToPoint(path, NULL,   52, 42 );
CGPathAddLineToPoint(path, NULL,   67, 20 );
CGPathAddLineToPoint(path, NULL,   92, 0 );
CGPathCloseSubpath(path);
于 2013-02-19T09:27:09.257 回答