1

请帮我解决这个困惑。

来自 Sprite Kit 编程指南:

sprite 节点的 anchorPoint 属性确定 帧中的哪个点位于 sprite 的位置。

我对此的理解是,如果我改变锚点,精灵的位置应该保持不变,只有纹理渲染应该相应地移动。

但是当我设置锚点时,我的精灵的位置实际上发生了变化!看看这个片段:

/* debug */
if (self.currentState == self.editState) {
    printf("B: relativeAnchorPoint = %.02f,%.02f  ", relativeAnchorPoint.x, relativeAnchorPoint.y);
    printf("position = %.02f,%.02f\n",self.position.x, self.position.y);
}

[self setAnchorPoint:relativeAnchorPoint];

/* debug */
if (self.currentState == self.editState) {
    printf("A: relativeAnchorPoint = %.02f,%.02f  ", relativeAnchorPoint.x, relativeAnchorPoint.y);
    printf("position = %.02f,%.02f\n",self.position.x, self.position.y);
}

输出:

A:relativeAnchorPoint = 0.65,0.48 位置 = 1532.00,384.00

B:relativeAnchorPoint = 0.65,0.48 位置 = 1583.00,384.00

我错过了什么?

提前致谢

*编辑:附加信息:* 仅当我的精灵将 xScale 设置为 -1 以反转图像时才会发生

4

1 回答 1

0

我做了一个快速测试来确认你的观察,它确实是正确的。

随着 xScale 变为负值,anchorPoint 实际上会影响节点的位置

我倾向于认为这是一个错误,因为负 xScale 与 x 位置的增加之间似乎没有相关性。这不能被视为正常行为。

此外,仅当您在 xScale 已经为负数之后更改 anchorPoint 时才会发生这种情况。您可以设置anchorPoint,然后根据需要更改xScale,一切都会好起来的,位置不会改变。

我确认 Xcode 5.1 (iOS 7) 和 Xcode 6 beta (iOS 8 beta) 中都存在这个问题。

如果您在新创建的 Sprite Kit 项目中运行以下代码来代替其自动创建的 MyScene.m 文件,您将看到随着 anchorPoint 在 0.0 和 1.0 之间随机变化,精灵的位置始终保持不变,直到 xScale 属性变为负值。此时 position.x 开始显着增加。

#import "MyScene.h"

@implementation MyScene
{
    SKSpriteNode *sprite;
}
-(id) initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        self.backgroundColor = [SKColor colorWithRed:0 green:0 blue:0.2 alpha:1];

        sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
        sprite.position = CGPointMake(CGRectGetMidX(self.frame),
                                       CGRectGetMidY(self.frame));
        sprite.anchorPoint = CGPointMake(0.2, 0.7);
        [self addChild:sprite];

        SKAction *action = [SKAction scaleXTo:-1.0 duration:10];
        [sprite runAction:[SKAction repeatActionForever:action]];
    }
    return self;
}
-(void) update:(CFTimeInterval)currentTime
{
    sprite.anchorPoint = CGPointMake(arc4random_uniform(10000) / 10000.0,
                                     arc4random_uniform(10000) / 10000.0);

    NSLog(@"pos: {%.1f, %.1f}, xScale: %.3f, anchor: {%.2f, %.2f}",
          sprite.position.x, sprite.position.y, sprite.xScale,
          sprite.anchorPoint.x, sprite.anchorPoint.y);
}
@end

这个错误有一个解决方法:

如果 xScale 已经是负数,则反转它,然后设置锚点,然后重新反转 xScale。如果 yScale 也可能变为负数,您可能需要对它执行相同的操作。

以下更新方法包含此解决方法,我确认这按预期工作:

-(void) update:(CFTimeInterval)currentTime
{
    BOOL didInvert = NO;
    if (sprite.xScale < 0.0)
    {
        didInvert = YES;
        sprite.xScale *= -1.0;
    }

    sprite.anchorPoint = CGPointMake(arc4random_uniform(10000) / 10000.0,
                                     arc4random_uniform(10000) / 10000.0);

    if (didInvert)
    {
        sprite.xScale *= -1.0;
    }

    NSLog(@"pos: {%.1f, %.1f}, xScale: %.3f, anchor: {%.2f, %.2f}",
          sprite.position.x, sprite.position.y, sprite.xScale, 
          sprite.anchorPoint.x, sprite.anchorPoint.y);
}

sprite.position 现在在整个 scaleXTo 动作持续时间内保持不变。

于 2014-07-30T09:41:00.733 回答