CCNode, abit , , . :)
1.x, 2.x. HD.
Just add this to your project and import it when you need it - it will be added to all classes originating from CCNode. (CCSprite, CCLayer)
Interface
#import "cocos2d.h"
@interface CCNode (Extensions)
-(CGPoint)positionOfAnchorPoint:(CGPoint)anchor;
-(CGPoint)positionOfAnchorPointInPoints:(CGPoint)anchor;
-(void)setAnchorPoint:(CGPoint)a lockPosition:(BOOL)lockPosition;
@end
Implementation
#import "CCNode+AnchorPos.h"
@implementation CCNode (Extensions)
-(CGPoint)positionOfAnchorPoint:(CGPoint)anchor
{
float x = anchor.x * self.contentSizeInPixels.width;
float y = anchor.y * self.contentSizeInPixels.height;
CGPoint pos = ccp(x,y);
pos = CGPointApplyAffineTransform(pos, [self nodeToParentTransform]);
return ccpMult(pos, 1/CC_CONTENT_SCALE_FACTOR());
}
-(CGPoint)positionOfAnchorPointInPoints:(CGPoint)anchor;
{
CGPoint anchorPointInPercent = ccp(anchor.x/self.contentSize.width, anchor.y/self.contentSize.height);
return [self positionOfAnchorPoint:anchorPointInPercent];
}
-(void)setAnchorPoint:(CGPoint)a lockPosition:(BOOL)lockPosition
{
CGPoint tempPos = [self positionOfAnchorPoint:a];
self.anchorPoint = a;
if(lockPosition)
{
self.position = tempPos;
}
}
@end
source
share