Note that there are some explanatory texts on larger screens.

plurals
  1. POtouch detection of ccnode anchored so 0,0 is the center?
    primarykey
    data
    text
    <p>I am trying to have ccnodes (sprites, labels, etc) individually respond to touch events...</p> <p>I have the following code:</p> <pre><code>@implementation CCNode (TouchDetection) (BOOL)containsPoint:(CGPoint)point padding:(NSArray *)padding { NSAssert(([padding count] == 4), @"padding must consist of [top, right, bottom, left] values!"); int paddingTop = [padding[0] integerValue]; int paddingRight = [padding[1] integerValue]; int paddingBottom = [padding[2] integerValue]; int paddingLeft = [padding[3] integerValue]; CGRect rect = CGRectMake(0, 0, paddingLeft + self.contentSize.width + paddingRight, paddingTop + self.contentSize.height + paddingBottom); return CGRectContainsPoint(rect, locationInNodeSpace); } -(BOOL)containsTouch:(UITouch *)touch padding:(NSArray *)padding { CCDirector* director = [CCDirector sharedDirector]; CGPoint locationGL = [director convertToGL:[touch locationInView:[[CCDirector sharedDirector] view]]]; return [self containsPoint:locationGL padding:padding]; } @end </code></pre> <p>This worked great except for on nodes that are anchored so that 0,0 is in the center. This implementation obviously won't work in this case due to the fact that it's being compared to a rectangle that has an origin of 0,0... So, when I tap on any of these nodes on the left side, they do not register because the locationInNodeSpace is negative, and therefore outside of the rectangle.</p> <p>I changed the code to:</p> <pre><code>(BOOL)containsPoint:(CGPoint)point padding:(NSArray *)padding { NSAssert(([padding count] == 4), @"padding must consist of [top, right, bottom, left] values!"); int paddingTop = [padding[0] integerValue]; int paddingRight = [padding[1] integerValue]; int paddingBottom = [padding[2] integerValue]; int paddingLeft = [padding[3] integerValue]; float paddedWidth = paddingLeft + self.contentSize.width + paddingRight; float paddedHeight = paddingTop + self.contentSize.height + paddingBottom; CGRect rect = CGRectMake(-paddedWidth / 2, -paddedHeight / 2, paddedWidth, paddedHeight); CGPoint locationInNodeSpace = [self convertToNodeSpace:point]; return CGRectContainsPoint(rect, locationInNodeSpace); } </code></pre> <p>And that fixed it for those cases, however now it is not working properly for nodes where 0,0 is not in their center.</p> <p>Am I going about this the wrong way? Is it just best to have everything be anchored the same?</p> <p><strong>UPDATE:</strong></p> <p>So apparently this problem has NOTHING to do with anchor points. I have a CCNode that has its contentSize set to 220,180. Its anchor point is 0,0... I added some NSLogging to see what's going on here. When I tap on the lower left corner, I see:</p> <pre><code>2013-09-28 09:24:11.205 [4101:c07] ================&gt; anchor:{0, 0} location in node:{-77, -65.5} location in world:{1131, 1298.5} rect:{{0, 0}, {220, 180}} boundingBox:{{220, 170}, {220, 180}} padding: 0 0 0 0 </code></pre> <p>It does NOT detect this touch, because the location in node space is NEGATIVE. Everything to the left and below of the center of the CCNode has a negative location.</p> <p>When I tap the upper right corner, I see:</p> <pre><code>2013-09-28 09:24:12.175 [4101:c07] ================&gt; anchor:{0, 0} location in node:{89, 64.5} location in world:{1297, 1428.5} rect:{{0, 0}, {220, 180}} boundingBox:{{220, 170}, {220, 180}} padding: 0 0 0 0 </code></pre> <p>So, using the CCNode's bounding box does not help me in anyway.. Modifying the rectangle based off of the anchor point does not help me in anyway.</p> <p>I need to understand why I am getting negative values... Can anyone please shed some light on this nonsense?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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