Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect transparent part on the sprite in cocos2d?

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.

like image 535
User1234 Avatar asked Sep 08 '12 19:09

User1234


People also ask

How to change the position of a sprite in cocos2d-x?

Changing the way a sprite (or any Node) is positioned is extremely simple in Cocos2d-x. This is done using something called an Anchor Point. Simply change the code like so: auto sprite = Sprite::create("decepticon.png");

How can I reduce the transparency of a sprite?

This reduces the amount of transparency by 24%. The even better solution is to create polygons matching the sprite's non-transparent areas. The good news is: You don't have to care about it when you are using TexturePacker —it can create rectangular and polygon trimmed shapes for your sprites in a format that cocos2d-x can read out of the box.

How to enable polygon packing In Cocos2D format?

Don't use the cocos2dformat —it does not contain polygon packing. Enable the polygon mode: Change the Trim modeto polygon. This also enables polygon packing. You can adjust the Tracer toleranceto change the amount of vertices created for the sprites. Tighter fitting polygons create less overdraw but have a higher vertex count.

How do I install the Cocos2D-x tutorial?

The tutorial is created using 3.16 - all parts except for the polygon packing should also work with older releases, too. Unzip the downloaded file and move in a location where you want to keep your installation. Open a command line interface and change the directory to the Cocos2d-x installation and run the setup cd cocos2d-x-3.16 python setup.py


2 Answers

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);
like image 150
Guru Avatar answered Oct 20 '22 20:10

Guru


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")

like image 40
Daniel Levin Avatar answered Oct 20 '22 20:10

Daniel Levin