0

我遇到的问题是 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);

在 ccTouchesBegan 触摸处理中,精灵的位置被列出在屏幕的左下角。

所以也许我错过了一些简单的东西,但有没有办法将精灵位置转换为与标签相同的位置,以识别标签字母上的触摸?

提前致谢。

4

4 回答 4

1

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

这应该解决它:

for (CCSprite *character in [label children])
{
   CGPoint touchLocationNodeSpace = [character convertToNodeSpace:touchLocation];
   if (CGRectContainsPoint([character boundingBox], touchLocationNodeSpace))
   {
      [character setColor:ccRED];
   }
}
于 2012-06-29T11:13:43.773 回答
0

多亏了您的建议,并进行了一些小的修改,我终于设法使其工作,我注意到的偏移是由于用于计算组成标签的每个字符的边界框的坐标的原点。

所以通过添加:

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

我在触摸检测中清除了这个偏移量。

这是 touches 的最终代码:

-(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];
        }
    }
}
于 2012-06-30T12:15:02.683 回答
0

这对我有用

CGRect boundingBox = [character boundingBox];
boundingBox.origin = CGPointZero;
于 2013-02-13T21:56:48.200 回答
0

不要用右手触摸左耳。
如果我是你,我会将它注册到这样的菜单中:(它还会即时添加缩放效果)

-(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
}
于 2013-07-08T10:15:14.747 回答