How do you know if you have contacted CCLabel?

How do you know if you have contacted CCLabel?

The following code obviously does not work well enough, because it only checks for point equality. Naturally, the touch point will not necessarily equal the CCLabel (CCNode) property of the position. How do I know if the Touch point has fallen into the "rectangle?" from CCLabel?

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
 for( UITouch *touch in touches ) {
  CGPoint location = [touch locationInView: [touch view]];

  location = [[CCDirector sharedDirector] convertToGL:location];

  self.myGraphManager.isSliding = NO;

  if(CGPointEqualToPoint(location, label1.position)){

   NSLog(@"Label 1 Touched");

  }else if(CGPointEqualToPoint(location, label2.position)){

   NSLog(@"Label 2 Touched");

  }else if(CGPointEqualToPoint(location, label3.position)){

   NSLog(@"Label 3 Touched");

  }else if(CGPointEqualToPoint(location, label4.position)){

   NSLog(@"Label 4 Touched");

  }else if(CGPointEqualToPoint(location, label5.position)){

   NSLog(@"Label 5 Touched");

  }else if(CGPointEqualToPoint(location, label6.position)){

   NSLog(@"Label 6 Touched");

  }

 }
}
+3
source share
1 answer

Use the CCLabel bounding box and test if the point is contained in rect using the Apple CGRectContainsPoint method, as described here: http://developer.apple.com/mac/library/documentation/GraphicsImaging/Reference/CGGeometry/Reference/reference.html# // apple_ref / c / func / CGRectContainsPoint

CCLabel, cocos2d, : http://www.learn-cocos2d.com/knowledge-base/cocos2d-iphone-faq/learn-cocos2d-public-content/manual/cocos2d-general/14813-how-to-get-a-sprites-bounding-box-bounding-rectangle-with-code

Objective-C CCSprite, CCSprite. CCLabel CCSprite, . :

CGRect bbox = [label getBoundingRect];
+3

Source: https://habr.com/ru/post/1746726/


All Articles