【问题标题】:How to tell if you touched a CCLabel?如何判断您是否触摸了 CCLabel?
【发布时间】:2010-05-23 06:27:40
【问题描述】:

如何判断您是否触摸了 CCLabel?

下面的代码显然不够好,因为它只测试点相等。自然接触点不一定等于 CCLabel (CCNode) 的位置属性。如何判断触摸点是否落在“矩形”内? 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");

  }

 }
}

【问题讨论】:

    标签: iphone xcode opengl-es cocos2d-iphone


    【解决方案1】:

    使用 CCLabel 的边界框并使用 Apple 的 CGRectContainsPoint 方法测试该点是否包含在矩形中,如下所述: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

    它将向 CCSprite 添加一个 Objective-C 类别,因此它的行为类似于 CCSprite 成员方法。由于 CCLabel 是 CCSprite 的子类,因此它也可以工作。你这样称呼它:

    CGRect bbox = [label getBoundingRect];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 2023-03-02
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      相关资源
      最近更新 更多