0

我创建了一个基于 CCNode 的操纵杆类,具有以下特征。

-(id)initWithLocation:(CGPoint)l
{
  if (self = [super init])
  {
    self.position = l;
    self.isRelativeAnchorPoint = NO;
    _background = [[[CCSprite alloc] initWithSpriteFrameName:@"JoystickBackground.png"] autorelease];
    [self addChild:_background];
    _stick = [[[CCSprite alloc] initWithSpriteFrameName:@"smallWhiteCircle.png"] autorelease];
    [self addChild:_stick];
    self.contentSize = _background.contentSize;
    _radius = self.contentSize.height / 2;    
  }

  return self;
}

问题是 joystick.boundingbox.origin 返回操纵杆的中点。是应返回左下角。是否可以为层配置它(即当 self.isRelativeAnchorPoint = NO 时)?

我通过使用以下函数覆盖边界框解决了这个问题

-(CGRect)boundingBox
{
  CGRect r;
  r.size = [self contentSize];
  CGPoint p = self.position;
  r.origin = ccp(p.x - r.size.width / 2, p.y - r.size.height / 2);

  return r;
}

但是有更好的方法吗?

我希望 _stick 位于 ccp(0. 0) 位置。这就是为什么我要使用层而不是普通的 CCNode 的原因。

4

1 回答 1

0

试试这个:

-(CGRect) boundingBox 
{
    if(isRelativeAnchorPoint)
      return [super boundingBox];

    CGRect sprRect = CGRectMake(
                                self.position.x - self.contentSize.width*self.anchorPoint.x,
                                self.position.y - self.contentSize.height*self.anchorPoint.y,
                                self.contentSize.width,
                                self.contentSize.height
                                );

    return sprRect;
}
于 2013-06-08T09:11:54.477 回答