2010-01-18 76 views
5

我正在嘗試更改我的Sprite錨點,以便旋轉0.0f,0.0f的錨點。起初,我的對象是在默認錨點(0.5f,0.5f)的旋轉。不過後來我需要它旋轉0.0,0.0 AnchorPoint。更改Sprite Anchorpoint而不移動它?

問題是我無法更改錨點並相應地更改位置,因此它保持在相同的位置,而不顯示對象快速移動並重新定位到其原始點。

有沒有一種方法可以一次設置我的Sprite的錨點和位置,而不會移動它?謝謝。

-Oscar

+0

你在說什麼樣的精靈? – 2010-01-18 21:34:41

+0

對不起,我忘了包括。我正在討論關於iPhone Sprites的openGL 2.0。 – 2010-01-20 14:57:17

+0

也許你的意思是cocos2d? OpenGL沒有「iPhone Sprite」。 – bentford 2010-04-14 08:44:44

回答

2

這是一個很好的問題,我不知道全部答案。

正如您可能已經注意到的那樣,在不影響比例和旋轉的情況下,不能更改anchorPoint。

調整後的精靈:

你必須同時改變anchorPoint和你的精靈的位置。見this question for a hint

對於旋轉精靈:

直覺說,你需要同時改變anchorPoint,旋轉和位置。 (我不知道如何計算這一點。)

注:我還在學習圖形編程,所以我不是100%能夠計算這個東西呢。

3

我找到了解決這個與一個UIView其他地方,並改寫了它的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

我相信你提到的「別處」是http://stackoverflow.com/a/5666430/108574。儘管如此,工作解決方案仍然是+1。 – 2012-08-06 13:44:18

2

我需要這幾次並決定爲CCNode一個擴展,測試了升技和似乎工作正常。可以真的有用:)

它用1.x測試,但它應該在2.x也很好。支持轉換節點和HD。

只需將其添加到您的項目並在需要時隨時導入 - 它將被添加到從CCNode派生的所有類中。 (CCSprite,CCLayer)

接口

#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 

實施

#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

非常感謝:D – 2014-07-10 17:06:36

0

的Cocos2D-X +固定規模

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; 
}