我正在尝试更改我的 Sprite anchor ,以便我可以在 0.0f,0.0f anchor 上旋转。起初我的对象是在默认 anchor (0.5f,0.5f)处旋转。但是稍后我需要它在 0.0,0.0 anchor 上旋转。
问题是我无法更改 anchor 并相应地更改位置,因此它保持在同一位置,而对象似乎没有快速移动并重新定位到其原始点。
有没有一种方法可以同时设置 anchor 和 Sprite 的位置,而 Sprite 根本不移动?谢谢。
-奥斯卡
最佳答案
我在其他地方用 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;
}
关于iphone - 在不移动的情况下更改 Sprite anchor ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2089285/