Detect transparent part on the sprite in cocos2d?

时光总嘲笑我的痴心妄想 提交于 2020-02-11 13:31:06

问题


I'm beginner in Cocos2d. I have a sprite, and I want to ignore touch on transparent area of that sprite.

I'm aware of this answer Cocos2d 2.0 - Ignoring touches to transparent areas of layers/sprites, and also this great article http://www.learn-cocos2d.com/2011/12/fast-pixelperfect-collision-detection-cocos2d-code-1of2/.

I was able to make it work with KKPixelMaskSprite, but only when sprite is used from file, but not from batch node. Whenever I use batch node (Sprite sheet), to get sprite , it stops working.

I have different sprites on each other, and I want detect this way -> if touch is in current sprite bounding box, is that part transparent on sprite or no?

P.S.I'm using cocos2d 1.0. I don't want to use any Physics engine for now, I just want to ignore touches on transparent areas of sprite (that was created using batch node ) .How can I do that? Or might there any tool can be helpfull?

Thanks a lot in advance.


回答1:


You can use CGMutablePathRef to make non-rectangular sprite collision detection.

//checking

    CGPoint loc =[mySprite convertToNodeSpace:touchPoint];

    if([mySprite isPointInsideMap:loc]) 
    {
         //touched inside..
    }

//Add this method in your MySprite class derived from CCSprite.
-(bool)isPointInsideMap:(CGPoint)inPoint
{
    if (CGPathContainsPoint(mCollisionPath, NULL, inPoint, NO) ) 
    {
        return true;
    }

    return false;
}

////Create Path

CGMutablePathRef  mCollisionPath = CGPathCreateMutable();
CGPathMoveToPoint(mCollisionPath,    NULL,  0, 0 );
CGPathAddLineToPoint(mCollisionPath, NULL,   11, 82 );
CGPathAddLineToPoint(mCollisionPath, NULL,   42, 152 );
CGPathAddLineToPoint(mCollisionPath, NULL,   86, 202 );
CGPathAddLineToPoint(mCollisionPath, NULL,   169, 13 );
CGPathCloseSubpath(mCollisionPath);



回答2:


This answer is more diffuse than you might expect, as I will not give you a code example, but this is how I'd implement this:

You have the location of the sprite's bounding box (corner of the sprite, including the transparency area if applicable), and the position of the touch on screen. Using this information, you can work out the location of the touch inside of the sprite. In other words, you can find the pixel touched, independant of the game screen.

Now that you have that pixel location (x and y), open the image (presumably a PNG), and get the RGB[A] value for that pixel. Each PNG has a transparency key. This is the alpha channel If the interior-pixel colour of the PNG at (x;y) == transparency key then that pixel is transparent

If you can get the alpha value for the pixel in question, if it is equal to 0 then the pixel is transparent.

edit: semantics ("alpha channel")




回答3:


I would try to change the boundingbox in your is Touch for me, and reduce it for the different sprites...



来源:https://stackoverflow.com/questions/12333822/detect-transparent-part-on-the-sprite-in-cocos2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!