Created
June 10, 2014 04:08
-
-
Save sarahsnow1/e27423aafaba3f0f82d5 to your computer and use it in GitHub Desktop.
SKNode+Helper.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "SKNode+Helper.h" | |
@implementation SKNode (Helper) | |
- (float)width { | |
return self.frame.size.width; | |
} | |
- (float)height { | |
return self.frame.size.height; | |
} | |
- (float)top { | |
return self.frame.origin.y; | |
} | |
- (float)bottom { | |
return self.top + self.height; | |
} | |
- (float)left { | |
return self.frame.origin.x; | |
} | |
- (float)right { | |
return self.left + self.width; | |
} | |
- (double)updatedXForNodeSubclasses:(double)x { | |
if ([self isKindOfClass:[SKLabelNode class]]) { | |
SKLabelNode *lblNode = (SKLabelNode *) self; | |
switch (lblNode.horizontalAlignmentMode) { | |
case SKLabelHorizontalAlignmentModeCenter: | |
return x + self.frame.size.width/2; | |
break; | |
case SKLabelHorizontalAlignmentModeRight: | |
return x + self.frame.size.width; | |
break; | |
default: | |
break; | |
} | |
} | |
if ([self isKindOfClass:[SKSpriteNode class]]) { | |
SKSpriteNode *spriteNode = (SKSpriteNode *) self; | |
CGPoint anchor = spriteNode.anchorPoint; | |
return x + self.frame.size.width * anchor.x; | |
} | |
if ([self isKindOfClass:[SKScene class]]) { | |
SKScene *sceneNode = (SKScene *) self; | |
CGPoint anchor = sceneNode.anchorPoint; | |
return x + self.frame.size.width * anchor.x; | |
} | |
if ([self isKindOfClass:[SKVideoNode class]]) { | |
SKVideoNode *videoNode = (SKVideoNode *) self; | |
CGPoint anchor = videoNode.anchorPoint; | |
return x + self.frame.size.width * anchor.x; | |
} | |
return x; | |
} | |
- (double)updatedYForNodeSubclasses:(double)y { | |
if ([self isKindOfClass:[SKLabelNode class]]) { | |
SKLabelNode *lblNode = (SKLabelNode *) self; | |
switch (lblNode.verticalAlignmentMode) { | |
case SKLabelVerticalAlignmentModeTop: | |
return y + self.frame.size.height; | |
break; | |
case SKLabelVerticalAlignmentModeCenter: | |
return y + self.frame.size.height/2; | |
break; | |
default: | |
break; | |
} | |
} | |
if ([self isKindOfClass:[SKSpriteNode class]]) { | |
SKSpriteNode *spriteNode = (SKSpriteNode *) self; | |
CGPoint anchor = spriteNode.anchorPoint; | |
return y + self.frame.size.height * anchor.y; | |
} | |
if ([self isKindOfClass:[SKScene class]]) { | |
SKScene *sceneNode = (SKScene *) self; | |
CGPoint anchor = sceneNode.anchorPoint; | |
return y + self.frame.size.height * anchor.y; | |
} | |
if ([self isKindOfClass:[SKVideoNode class]]) { | |
SKVideoNode *videoNode = (SKVideoNode *) self; | |
CGPoint anchor = videoNode.anchorPoint; | |
return y + self.frame.size.height * anchor.y; | |
} | |
return y; | |
} | |
- (void)centerXInParentAndSetY:(int)y { | |
if (!self.parent) { | |
return; | |
} | |
CGRect myFrame = self.frame; | |
CGRect parentFrame = self.parent.frame; | |
double x = (parentFrame.size.width - myFrame.size.width)/2; | |
x = [self updatedXForNodeSubclasses:x]; | |
self.position = CGPointMake(x, y); | |
} | |
- (void)centerXInParent { | |
[self centerXInParentAndSetY:self.position.y]; | |
} | |
- (void)centerYInParentAndSetX:(int)x { | |
if (!self.parent) { | |
return; | |
} | |
CGRect myFrame = self.frame; | |
CGRect parentFrame = self.parent.frame; | |
double y = (parentFrame.size.height - myFrame.size.height)/2; | |
y = [self updatedYForNodeSubclasses:y]; | |
self.position = CGPointMake(x, y); | |
} | |
- (void)centerYInParent { | |
[self centerYInParentAndSetX:self.position.y]; | |
} | |
- (void)centerInParent { | |
if (!self.parent) { | |
return; | |
} | |
CGRect myFrame = self.frame; | |
CGRect parentFrame = self.parent.frame; | |
double x = (parentFrame.size.width - myFrame.size.width)/2; | |
double y = (parentFrame.size.height - myFrame.size.height)/2; | |
x = [self updatedXForNodeSubclasses:x]; | |
y = [self updatedYForNodeSubclasses:y]; | |
self.position = CGPointMake(x, y); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment