2010-01-18 42 views
5

Tôi đang cố gắng thay đổi điểm neo Sprite của mình để tôi có thể xoay qua điểm neo 0.0f, 0.0f. Lúc đầu đối tượng của tôi là xoay tại điểm neo mặc định (0.5f, 0.5f). Tuy nhiên sau này tôi cần nó để xoay qua một AnchorPoint 0.0.0.0.Thay đổi Sprite Anchorpoint mà không cần di chuyển nó?

Vấn đề là tôi không thể thay đổi điểm neo và thay đổi vị trí tương ứng, do đó, nó vẫn ở trên cùng một vị trí, không có đối tượng xuất hiện để di chuyển và định vị lại vị trí ban đầu.

Có cách nào tôi có thể đặt điểm neo và vị trí của Sprite của tôi cùng một lúc, mà không cần di chuyển chút nào không ?. Cảm ơn bạn.

-Oscar

+0

Bạn đang nói về loại sprites nào? –

+0

Xin lỗi tôi quên bao gồm điều đó. Tôi đang nói về OpenGL 2.0 cho iPhone Sprites. –

+0

Có lẽ bạn muốn nói đến cocos2d? OpenGL không có "iPhone Sprite". – bentford

Trả lời

2

Đây là một câu hỏi hay, và tôi không biết câu trả lời đầy đủ được nêu ra.

Như bạn có thể đã nhận thấy, không thể thay đổi anchorPoint mà không ảnh hưởng đến tỷ lệ và xoay vòng.

Đối với sprites quy mô:

Bạn phải đồng thời thay đổi anchorPoint và vị trí của sprite của bạn. Xem this question for a hint

Đối với sprites xoay:

Trực giác nói rằng bạn sẽ cần phải đồng thời thay đổi anchorPoint, luân chuyển, và vị trí. (Tôi không có ý tưởng làm thế nào để tính toán này.)

LƯU Ý: tôi vẫn đang học lập trình đồ họa, vì vậy tôi không chắc chắn 100% có thể tính toán cụ này được nêu ra.

3

Tôi tìm thấy một giải pháp cho điều này với một UIView nơi khác, và viết lại nó cho cocos2d:

- (void)setAnchorPoint:(CGPoint)anchorPoint forSprite:(CCSprite *)sprite 
{ 
    CGPoint newPoint = CGPointMake(sprite.contentSize.width * anchorPoint.x, sprite.contentSize.height * anchorPoint.y); 
    CGPoint oldPoint = CGPointMake(sprite.contentSize.width * sprite.anchorPoint.x, sprite.contentSize.height * sprite.anchorPoint.y); 

    newPoint = CGPointApplyAffineTransform(newPoint, [sprite nodeToWorldTransform]); 
    oldPoint = CGPointApplyAffineTransform(oldPoint, [sprite nodeToWorldTransform]); 

    CGPoint position = sprite.position; 

    position.x -= oldPoint.x; 
    position.x += newPoint.x; 

    position.y -= oldPoint.y; 
    position.y += newPoint.y; 

    sprite.position = position; 
    sprite.anchorPoint = anchorPoint; 
} 
+0

Tôi tin rằng "ở nơi khác" mà bạn đã đề cập là http://stackoverflow.com/a/5666430/108574. Tuy nhiên, vẫn còn 1 cho giải pháp làm việc. –

2

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 
+0

Cảm ơn bạn rất nhiều: D –

0

Cocos2d-x + Cố định quy mô

YourClass.h

virtual cocos2d::Vec2 positionFromSprite(cocos2d::Vec2 newAnchorPoint, cocos2d::Sprite *sprite); 

YourClass.m

Vec2 YourClass::positionFromSprite(Vec2 newAnchorPoint, cocos2d::Sprite *sprite) { 
    Rect rect = sprite->getSpriteFrame()->getRect(); 
    Vec2 oldAnchorPoint = sprite->getAnchorPoint(); 
    float scaleX = sprite->getScaleX(); 
    float scaleY = sprite->getScaleY(); 

    Vec2 newPoint = Vec2(rect.size.width * newAnchorPoint.x * scaleX, rect.size.height * newAnchorPoint.y * scaleY); 
    Vec2 oldPoint = Vec2(rect.size.width * oldAnchorPoint.x * scaleX, rect.size.height * oldAnchorPoint.y * scaleY); 

    Vec2 position = sprite->getPosition(); 

    position.x -= oldPoint.x; 
    position.x += newPoint.x; 

    position.y -= oldPoint.y; 
    position.y += newPoint.y; 

    return position; 
} 
Các vấn đề liên quan