tôi đã cần thiết này một vài lần và quyết định để thực hiện một phần mở rộng cho CCNode, thử nghiệm nó ABIT và dường như làm việc tốt. Có thể thực sự hữu ích đối với một số người :)
Nó được thử nghiệm với 1.x nhưng nó cũng hoạt động tốt trong 2.x. Hỗ trợ các nút chuyển đổi và HD.
Chỉ cần thêm điều này vào dự án của bạn và nhập bất cứ khi nào bạn cần - Dự án sẽ được thêm vào tất cả các lớp bắt nguồn từ 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
Thực hiện
#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
Nguồn
2012-11-04 04:55:52
Bạn đang nói về loại sprites nào? –
Xin lỗi tôi quên bao gồm điều đó. Tôi đang nói về OpenGL 2.0 cho iPhone Sprites. –
Có lẽ bạn muốn nói đến cocos2d? OpenGL không có "iPhone Sprite". – bentford