Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何判断您是否触摸了CCL标签?_Iphone_Xcode_Opengl Es_Cocos2d Iphone - Fatal编程技术网

Iphone 如何判断您是否触摸了CCL标签?

Iphone 如何判断您是否触摸了CCL标签?,iphone,xcode,opengl-es,cocos2d-iphone,Iphone,Xcode,Opengl Es,Cocos2d Iphone,如何判断您是否触摸了CCL标签 下面的代码显然不够好,因为它只测试点的相等性。自然,接触点不一定等于CCLabel(CCNode)的位置属性。如何判断触摸点是否落在CCL标签的“矩形”内 - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { for( UITouch *touch in touches ) { CGPoint location = [touch locationInView: [touch

如何判断您是否触摸了CCL标签

下面的代码显然不够好,因为它只测试点的相等性。自然,接触点不一定等于CCLabel(CCNode)的位置属性。如何判断触摸点是否落在CCL标签的“矩形”内

- (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");

  }

 }
}

使用CCLabel的边界框,并使用Apple的CGRectContainsPoint方法测试该点是否包含在rect中,如下所述:

要获取CCLabel的边框,请遵循我的cocos2d常见问题解答中的以下建议,即如何获取精灵的边框矩形:

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

CGRect bbox = [label getBoundingRect];