0

我已经被困了好几个星期,现在试图弄清楚如何忽略对精灵透明区域的触摸。我一直在尝试使用本教程来跟踪像素完美碰撞 - 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 但这似乎也不起作用。我怎么能忽略对精​​灵透明部分的触摸?请帮我。

4

1 回答 1

1

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

请参阅我在此线程中的答案。

有关如何创建 CGPath 的信息:这里

欲了解更多信息,请点击这里

于 2013-02-18T10:10:37.797 回答