touch detection of non-rectangular sprites (cocos2d)
- by hogni89
What is the correct way to implement a non-rectangular sprite in Cocos2d?
I am working on a jigsaw puzzle. And therefor do our sprites have some strange forms (Jigsaw puzzle bricks). As of now, we have implemented the "detect" this way:
- (void)selectSpriteForTouch:(CGPoint)touchLocation {
CCSprite * newSprite = nil;
// Loop array of sprites
for (CCSprite *sprite in movableSprites) {
// Check if sprite is hit.
// TODO: Swap if with something better.
if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {
newSprite = sprite;
break;
}
}
if (newSprite != selSprite) {
// Move along, nothing to see here
// Not the problem
}
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
[self selectSpriteForTouch:touchLocation];
return TRUE;
}
I know that the problem is in the keyword "sprite.boundingBox". Is there a better way of implementing this, OR is it a limit when using sprites based on .png's?
If so, how should I proceed?
I'm new to iPhone and game development :D