在cocos2d中未正确检测到CGPath [英] CGPath is not detected properly in cocos2d

查看:146
本文介绍了在cocos2d中未正确检测到CGPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这适用于所有其他类,除了一个。无论我在哪里,xcode使用以下代码保持打印外部:

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

我只能移动我的sprite如果条件,不是别的,即使我点击实际的彩色精灵,它不会得到我设置的路径。我的测量是否错误?如果不是,我做错了什么?这与我要使用的图片相似...

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...

推荐答案

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

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

这篇关于在cocos2d中未正确检测到CGPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆