/ Published in: Objective C
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#pragma mark CCDirector Category for Point Handling #import "cocos2d.h" @interface CCDirector (Points) - (CGSize) ptWinSize; - (CGPoint) convertToPt:(CGPoint)point; - (CGPoint) convertToPx:(CGPoint)point; @end @implementation CCDirector (Points) - (CGSize) ptWinSize { CGSize s = [self winSize]; CGPoint p = [self convertToPt:ccp(s.width, s.height)]; return CGSizeMake(p.x, p.y); } - (CGPoint) convertToPt:(CGPoint)point { return ccpMult(point, 1.0/self.contentScaleFactor); } - (CGPoint) convertToPx:(CGPoint)point { return ccpMult(point, self.contentScaleFactor); } @end #pragma mark CCNode Category for Point Handling @interface CCNode (Points) @property (assign) CGPoint ptPosition; @property (assign) CGSize ptContentSize; - (CGPoint) ptConvertToNodeSpace:(CGPoint)point; - (CGPoint) ptConvertToWorldSpace:(CGPoint)point; @end @implementation CCNode (Points) - (void) setPtPosition:(CGPoint)pos { self.position = [[CCDirector sharedDirector] convertToPx:pos]; } - (CGPoint) ptPosition { return [[CCDirector sharedDirector] convertToPt:self.position]; } - (void) setPtContentSize:(CGSize)size { CGPoint p = [[CCDirector sharedDirector] convertToPx:ccp(size.width, size.height)]; self.contentSize = CGSizeMake(p.x, p.y); } - (CGSize) ptContentSize { CGPoint p = [[CCDirector sharedDirector] convertToPt:ccp(self.contentSize.width, self.contentSize.height)]; return CGSizeMake(p.x, p.y); } - (CGPoint) ptConvertToNodeSpace:(CGPoint)point { point = [[CCDirector sharedDirector] convertToPx:point]; point = [self convertToNodeSpace:point]; point = [[CCDirector sharedDirector] convertToPt:point]; return point; } - (CGPoint) ptConvertToWorldSpace:(CGPoint)point { point = [[CCDirector sharedDirector] convertToPx:point]; point = [self convertToWorldSpace:point]; point = [[CCDirector sharedDirector] convertToPt:point]; return point; } @end