Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've needed this a couple of times and decided to make a extension for CCNode, tested it abit and seems to work fine. Can be really useful to some :)</p> <p>It's tested with 1.x but It should work fine in 2.x too. Supports transformed nodes and HD.</p> <p>Just add this to your project and import whenever you need it - It will be added to all classes deriving from CCNode. (CCSprite, CCLayer)</p> <p><strong>Interface</strong></p> <pre><code>#import "cocos2d.h" @interface CCNode (Extensions) // Returns the parent coordinate for an anchorpoint. Useful for aligning nodes with different anchorpoints for instance -(CGPoint)positionOfAnchorPoint:(CGPoint)anchor; // As above but using anchorpoint in points rather than percentage -(CGPoint)positionOfAnchorPointInPoints:(CGPoint)anchor; //Sets the anchorpoint, to not move the node set lockPosition to `YES`. Setting it to `NO` is equal to setAnchorPoint, I thought this would be good for readability so you always know what you do when you move the anchorpoint -(void)setAnchorPoint:(CGPoint)a lockPosition:(BOOL)lockPosition; @end </code></pre> <p><strong>Implementation</strong></p> <pre><code>#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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload