Skip to content

Instantly share code, notes, and snippets.

@sdabet
Last active December 10, 2015 21:28
Show Gist options
  • Save sdabet/4495065 to your computer and use it in GitHub Desktop.
Save sdabet/4495065 to your computer and use it in GitHub Desktop.
@implementation CCNode (centering)
-(void) centerContentWithPadding:(int)padding TotalWidth:(int)totalWidth{
int contentWidth = 0;
for(int i=0; i < self.children.count; i++) {
CCNode *child = [self.children objectAtIndex:i];
contentWidth += child.contentSize.width;
}
int currentX = (totalWidth - contentWidth) / 2;
for(int i=0; i < self.children.count; i++) {
CCNode *child = [self.children objectAtIndex:i];
child.position = ccp(currentX + child.anchorPoint.x * child.contentSize.width, child.position.y);
currentX += child.contentSize.width + padding;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment