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

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

问题描述

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

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

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

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天全站免登陆