我创建了一个基于 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 的原因。