I'm trying to change the anchor point of my node and keep the "position" on the screen the same. I know I'll need to set the anchor point and calculate the new position then set that at the same time, but I seem to have a math-deficiency and no matter what I do the sprite still moves. Has anyone done this? What calculations do I need to perform to the position to keep the sprite in the same place on the screen?
Change anchor point and keep position
(3 posts) (2 voices)-
Posted 1 year ago #
-
This is the instant action I use to change the anchorpoint without a position change. It's coded for 0.99.4, so you'll have to rename instantAction to actionInstant.
.h
@interface CCChangeAnchorPoint : CCInstantAction <NSCopying> { CGPoint newAP; BOOL absolute; //YES = absolute, NO = anchorpoint relative } +(id) actionWithAnchorPoint: (CGPoint) anchor withAbsolutePoint:(BOOL) isAbsolute; -(id) initWithAnchorPoint:(CGPoint) anchor withAbsolutePoint:(BOOL) isAbsolute; -(void) startWithTarget:(id)aTarget; @end.m
+(id) actionWithAnchorPoint: (CGPoint) anchor withAbsolutePoint:(BOOL) isAbsolute { return [[[self alloc] initWithAnchorPoint:anchor withAbsolutePoint:isAbsolute]autorelease]; } -(id) initWithAnchorPoint:(CGPoint) anchor withAbsolutePoint:(BOOL) isAbsolute { if( (self=[super init]) ) { newAP = anchor; absolute = isAbsolute; } return self; } -(id) copyWithZone: (NSZone*) zone { CCInstantAction *copy = [[[self class] allocWithZone: zone] initWithAnchorPoint: newAP withAbsolutePoint:absolute]; return copy; } -(void) startWithTarget:(id)aTarget { [super startWithTarget:aTarget]; CCNode *node=(CCNode*) aTarget; CGPoint oldPosition=node.anchorPointInPixels; if (!absolute) newAP=ccpAdd(node.anchorPointInPixels,newAP); [node setAnchorPoint:ccp(newAP.x / (node.contentSize.width*node.scaleX),newAP.y / (node.contentSize.height*node.scaleY))]; CGPoint translate= ccpSub([node convertToWorldSpace:newAP],[node convertToWorldSpace:oldPosition]); [node setPosition:ccpAdd(node.position,translate)]; } @endPosted 1 year ago # -
Brilliant++
Posted 1 year ago #
Reply
You must log in to post.