Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try using the CCNode convertTouchToNodeSpaceAR: method to convert the point to the rotated coordinates and then you can do the compare of the sprite bounds.</p> <p>I made this a category on CCNode so it's available to any CCNode or subclass.</p> <pre><code>@interface CCNode (gndUtils) // Lets a node test to see if a touch is in it. // Takes into account the scaling/rotation/transforms of all // the parents in the parent chain. // Note that rotation of a rectangle doesn't produce a rectangle // (and we are using a simple rectangle test) // so this is testing the smallest rectangle that encloses the rotated node. // This does the converstion to view and then world coordinates // so if you are testing lots of nodes, do that converstion manually // // CGPoint touchLoc = [touch locationInView: [touch view]]; // convert to "View" // touchLoc = [[CCDirector sharedDirector] convertToGL: touchLoc]; // move to "World" // and then use worldPointInNode: method instead for efficiency. - (BOOL) touchInNode: (UITouch *) touch; // allows a node to test if a world point is in it. - (BOOL) worldPointInNode: (CGPoint) worldPoint; @end </code></pre> <p>and the implementation:</p> <pre><code>@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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload