我做了一个快速测试来确认你的观察,它确实是正确的。
随着 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 动作持续时间内保持不变。