J'ai besoin de ce deux ou trois fois et a décidé de faire une extension pour CCNode, testé et ABIT semble fonctionner correctement. Peut être vraiment utile à certains :)
Il a été testé avec 1.x mais il devrait fonctionner correctement dans 2.x aussi. Prend en charge les nœuds transformés et HD. Ajoutez simplement ceci à votre projet et importez quand vous en avez besoin - Il sera ajouté à toutes les classes dérivant de CCNode. (CCSprite, CCLayer)
Interface
#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
mise en oeuvre
#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
De quel type de sprites parlez-vous? –
Désolé j'ai oublié d'inclure cela. Je parle d'OpenGL 2.0 pour iPhone Sprites. –
Peut-être que vous voulez dire cocos2d? OpenGL n'a pas de "iPhone Sprite". – bentford