在cocos2d中未正确检测到CGPath

时间:2013-02-19 09:09:08

标签: objective-c cocos2d-iphone cgpath

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

    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条件,我只能移动我的精灵,除此之外,即使我点击实际的彩色精灵,它也没有得到我设置的路径。我的测量结果错了吗?如果没有,我做错了什么?这与我试图使用的图像相似......

sample

1 个答案:

答案 0 :(得分:1)

仅当图像大小相同时才有效。根据图像大小计算坐标。

enter image description here

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);