Skip to content

Instantly share code, notes, and snippets.

@zaneclaes
Created November 10, 2013 16:55
Show Gist options
  • Save zaneclaes/7400737 to your computer and use it in GitHub Desktop.
Save zaneclaes/7400737 to your computer and use it in GitHub Desktop.
Detecting hit areas on a cocos2d sprite
@implementation MySprite
// The internal rect representing the current texture
- (CGRect)hitArea {
CGPoint bl = CGPointMake(MIN(_quad.tl.vertices.x, _quad.bl.vertices.x), MIN(_quad.bl.vertices.y, _quad.br.vertices.y));
CGPoint tr = CGPointMake(MAX(_quad.tr.vertices.x, _quad.br.vertices.x), MAX(_quad.tl.vertices.y, _quad.tr.vertices.y));
return CGRectMake(bl.x, bl.y, tr.x - bl.x, tr.y - bl.y);
}
// Works just like .boundingBox (eg, represents the hitArea in parent coordinates)
- (CGRect)hitBox {
return CGRectApplyAffineTransform(self.textureArea, [self nodeToParentTransform]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment