Make the below category on CCNode:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@interface CCNode (gndUtils) - (BOOL) touchInNode: (UITouch *) touch; // allows a node to test if a world point is in it. - (BOOL) worldPointInNode: (CGPoint) worldPoint; @ Implementation: @implementation CCNode (gndUtils) - (BOOL) touchInNode: (UITouch *) touch { CGPoint touchLoc = [touch locationInView: [touch view]]; // convert to "View coordinates" from "window" presumably touchLoc = [[CCDirector sharedDirector] convertToGL: touchLoc]; // move to "cocos2d World coordinates" return [self worldPointInNode: touchLoc]; } - (BOOL) worldPointInNode: (CGPoint) worldPoint { // scale the bounding rect of the node to world coordinates so we can see if the worldPoint is in the node CGRect bbox = CGRectMake( 0.0f, 0.0f, self.contentSize.width, self.contentSize.height ); // get bounding box in local bbox = CGRectApplyAffineTransform(bbox, [self nodeToWorldTransform] ); // convert box to world coordinates, scaling etc. return CGRectContainsPoint( bbox, worldPoint ); } @end |
Incoming search terms:
- @interface CCNode (gndUtils)
Filed under: Cocos2d
Like this post? Subscribe to my RSS feed and get loads more!




Leave a Reply