在cocos2d中忽略sprite的透明区域

时间:2013-02-18 02:47:04

标签: objective-c cocos2d-iphone

我已经被困了几个星期,现在试图找出如何忽略对精灵透明区域的触摸。我一直在尝试使用本教程跟踪像素完美碰撞 - http://www.learn-cocos2d.com/2011/12/fast-pixelperfect-collision-detection-cocos2d-code-1of2/无济于事。这是我的代码目前的样子:

-(void)checkTap:(CGPoint)touch{
BOOL yesNo = NO;
if(yesNo == NO)
{
    sprTap.position = ccp(touch.x, touch.y);
}}

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];

[self checkTap:location];
touchFlag = 0;

for(int i = 0; i < [sprArray count]; i++)
{
  KKPixelMaskSprite *sprite = (KKPixelMaskSprite *)[sprArray objectAtIndex:i];
        if([sprTap intersectsNode:sprite])
        {
            selectedSprite = sprite;
            touchFlag = 1;
            break;
        }
}}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];

[sprTap setPosition:location];
if(touchFlag == 1)
{
  _spriteTouch = TRUE;
  [selectedSprite setPosition:location];
}
else
{
  for(int i = 0; i < [sprArray count]; i++)
  {
      KKPixelMaskSprite *sprite = (KKPixelMaskSprite *)[sprArray objectAtIndex:i];
                if([sprTap intersectsNode:sprite])
                {
                    selectedSprite = sprite;
                    touchFlag = 1;
                    break;
                }
            }
        }
}}

这个问题是当sprTap的边界框与精灵的边界框相交时,它会同时移动,因为我的精灵不是完美的正方形,这是行不通的。我也尝试过pixelMaskIntersectsNode,但这似乎也不起作用。我怎么能忽略精灵透明部分的触摸呢?请帮帮我。

1 个答案:

答案 0 :(得分:1)

您可以使用CGMutablePathRef来检测透明部分:

请参阅this thread.

中的答案

有关如何创建CGPath的信息:Here

有关详细信息,请Click Here