CGPath is not detected properly in cocos2d

∥☆過路亽.° 提交于 2019-12-20 02:56:14

问题


Through help and suggestions, I created a path for my sprite so that only the non-transparent parts can be touched. This is the path I came up with:

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

This works for all my other classes except for one. No matter where I tap, xcode keeps printing "outside" using this code:

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

I can only move my sprites if I get inside the if-condition, not else but even if I tap on the actual, colored sprite, it doesn't get the path I set. Are my measurements wrong? If not, what am I doing wrong? This is similar to the image I'm trying to use...


回答1:


This works only if image size is same. Depending on image size calculate coordinates.

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


来源:https://stackoverflow.com/questions/14953630/cgpath-is-not-detected-properly-in-cocos2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!