2010-01-18 58 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; 
}