Cocos2d iphone 在CCLabelBMFont标签的CCSprite字符上注册接触

Cocos2d iphone 在CCLabelBMFont标签的CCSprite字符上注册接触,cocos2d-iphone,label,ccsprite,touches,Cocos2d Iphone,Label,Ccsprite,Touches,我遇到的问题是,CCLabelBMFont标签的位置和组成该标签的CCSprite字符的位置似乎不同,因此我无法管理触摸事件 为了更详细地测试此问题,我尝试了以下方法: -(id)init { if ((self = [super init]) { CCLabelBMFont *label = [CCLabelBMFont labelWithString:"welcome" fntFile:@"Arial.fnt"]; CG

我遇到的问题是,CCLabelBMFont标签的位置和组成该标签的CCSprite字符的位置似乎不同,因此我无法管理触摸事件

为了更详细地测试此问题,我尝试了以下方法:

    -(id)init
    {
      if ((self = [super init])
      {
        CCLabelBMFont *label = [CCLabelBMFont labelWithString:"welcome" fntFile:@"Arial.fnt"];
        CGSize size = [[CCDirector sharedDirector] winSize];
        label.position =  ccp(size.width/2, size.height/2);
        [self addChild: self.label];

        self.isTouchEnabled = YES;
      }
    return self;
    }

    -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
      UITouch *touch = [touches anyObject];
      CGPoint touchLocation = [touch locationInView:[touch view]];
      touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
      NSLog(@"point touched: x:%f, y:%f", touchLocation.x, touchLocation.y);

      for (CCSprite *character in [label children])
      {
        if (CGRectContainsPoint([character boundingBox], touchLocation))
        {
          [character setColor:ccRED];
        }
      }
    }
无论我触摸标签上的字母是什么,它都没有变成应有的红色

我认为构成CCLabelBMFont和标签本身的精灵位置不同的原因是,当我插入:

NSLog(@"character position: x:%f y: %f", character.position.x, character.position.y);
在CCToucheSBegind touch handling中,精灵的位置列在屏幕的左下角

因此,也许我遗漏了一些简单的东西,但是有没有一种方法可以将精灵的位置转换为与标签相同的位置,以便识别标签字母上的触摸


提前感谢。

子节点的位置相对于其父节点。必须使用精灵的convertToNodeSpace或convertToWorldSpace方法相应地转换位置。在这种情况下,在使用convertToNodeSpace测试边界框碰撞之前,必须使用convertToNodeSpace和touchLocation作为每个精灵的输入

这应该可以解决这个问题:

for (CCSprite *character in [label children])
{
   CGPoint touchLocationNodeSpace = [character convertToNodeSpace:touchLocation];
   if (CGRectContainsPoint([character boundingBox], touchLocationNodeSpace))
   {
      [character setColor:ccRED];
   }
}

多亏了你的建议,经过一个小小的修改,我终于成功地使它工作了,我注意到的偏移是由于用于计算构成标签的每个字符的边界框的坐标原点造成的

因此,加入:

CGRect boundingBox = [character boundingBox];
boundingBox.origin = CGPointZero;
我在触摸检测中清除了这个偏移

以下是触摸的最终代码:

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:[touch view]];
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];

    for (CCSprite *character in [self.label children])
    {
        CGRect boundingBox = [character boundingBox];
        boundingBox.origin = CGPointZero;
        CGPoint touchLocationNodeSpace = [character convertToNodeSpace:touchLocation];
        if (CGRectContainsPoint(boundingBox, touchLocationNodeSpace))
        {
            [character setColor:ccRED];
        }
    }
}

这对我有好处

CGRect boundingBox = [character boundingBox];
boundingBox.origin = CGPointZero;

不要用右手触摸左耳。
如果我是你,我会将其注册到如下菜单: (还可以动态添加缩放效果)


非常感谢,现在它对我单词的第一个字母非常有效,但是对于其他字母,我需要在显示单词的右侧进一步触摸,使它们也变成红色,你知道它来自哪里吗?
-(void)GenerateButtons
{
  CGSize s = [[CCDirector sharedDirector] winSize];
  CCLabelBMFont *myBtn=[CCLabelBMFont labelWithString:@"BLABLA" fntFile:@"comicsansms.fnt"];
  CCMenuItemLabel *lbl_=[CCMenuItemLabel itemWithLabel:shakeBtn target:self selector:@selector(myButtonPressed)];
CCMenu *menu_=[CCMenu menuWithItems:lbl_, nil];
[self addChild:menu_];
[menu_ setPosition:ccp(s.width/2,(myBtn.boundingBox.size.height))];
}

-(void)myButtonPressed{
   //do what your CCLabelBMFont should do
}