我已经基于具有以下特征的CCNode创建了一个Joystick类。
-(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时)?
我通过使用以下函数覆盖boundingbox来解决这个问题
-(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。
答案 0 :(得分: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;
}