iphone - 在不移动的情况下更改 Sprite anchor ?

标签 iphone objective-c cocos2d-iphone

我正在尝试更改我的 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/

相关文章:

ios - 在 UIView 的子类中添加按钮和操作

ios - Reactive Cocoa 在没有代码重复的情况下拆分信号?

javascript - 在 Python 程序中包装 Canvas 标签 + javascript 的最简单方法?

c - 找到一个 vector 在可变轴上的角度

cocos2d-iphone - 如何在cocos2d创建的项目中使用UISwipeGestureRecognizer?

iphone - UIImagePickerController cameraOverlayView在iPad上无法全屏

iphone - 完成后未删除时如何重用 CABasicAnimation?

iphone - 如何在相关 View Controller 中获取我的 tabbarcontroller 的选定索引?

iphone - 短信关键词 : Geo

objective-c - 在 Cocoa 中创建 Excel (.xlsx) 文件