忽略cocos2d中的sprites的透明区域 [英] Ignoring transparent areas of sprites in cocos2d

查看:342
本文介绍了忽略cocos2d中的sprites的透明区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困住了几个星期,现在试图弄清楚如何忽略触摸到透明区域的精灵。我一直在尝试使用本教程跟踪像素完美碰撞 - http://www.learn-cocos2d.com/2011/12/fast-pixelperfect-collision-detection-cocos2d-code-1of2/ 无效。这是目前我的代码看起来:

I've been stuck for literally weeks now trying to figure out how I can ignore touches to transparent areas of a sprite. I've been trying to follow pixel perfect collisions using this tutorial -- http://www.learn-cocos2d.com/2011/12/fast-pixelperfect-collision-detection-cocos2d-code-1of2/ to no avail. This is currently how my code looks:

-(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的边界框与sprite的边界框相交时,它移动两者,既然我的sprite不是完美的正方形,那就不会了。我也试过pixelMaskIntersectsNode但这似乎也不工作。我还可以忽略对sprite的透明部分的触摸?请帮助我。

The problem with this is when the bounding box of sprTap intersects with the bounding box of sprite, it moves both and since my sprites aren't perfect squares, that won't do. I also tried pixelMaskIntersectsNode but that doesn't seem to work either. How else can I ignore the touches to the transparent parts of the sprite? Please help me.

推荐答案

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

You can use CGMutablePathRef to detect transparent part:

请参阅此主题中的答案。

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

Information about How to create CGPath:Here

有关详情,点击此处

这篇关于忽略cocos2d中的sprites的透明区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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