Reputation: 2221
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;
}
}
}
}}
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.
Upvotes: 0
Views: 555
Reputation: 22042
You can use CGMutablePathRef to detect transparent part:
Refer my answer in this thread.
Information about How to create CGPath:Here
For more information, Click Here
Upvotes: 1