在圆圈中移动精灵

时间:2014-07-23 15:28:03

标签: objective-c xcode sprite-kit

我正在尝试将一个Sprite(" Spaceship")移动到一个圆圈中,执行点击。

以下是执行点击时触发的代码:

- (void)newSpaceShipAt: (CGPoint)location
{
    SKSpriteNode *hull = [[SKSpriteNode alloc]initWithImageNamed:@"Spaceship"];
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddArc(path, NULL, location.x, location.y, self.size.width / 5.0, 0, 360, YES);

    SKAction *move = [SKAction sequence:@[
                                          [SKAction followPath:path duration:1],
                                          ]];

    [hull runAction:[SKAction repeatActionForever:move]];
    hull.name = @"Spaceship";
    hull.scale = 0.5;

    hull.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:MAX(hull.size.width / 2.0, hull.size.height / 2.0)];
    hull.physicsBody.dynamic = NO;

    [self addChild:hull];
}

我是太空飞船无限期地在圆圈中移动点#34;位置",这是执行点击的地方。然而,发生的事情是,在一次成功的革命之后,宇宙飞船会移动"位置"相对于点击。例如,如果在(50,100)上执行了点击,那么在一次旋转之后它将相对于场景移动到(100,200),并且它将继续。

我该怎么做才能解决这个问题?或者如果我的方法完全错误,那会更好吗?

此外,绘制轨迹(简单线条)到精灵运动的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

来自followPath:duration: ...

的文档
  

<强>讨论   调用此方法相当于调用   followPath:asOffset:orientToPath:duration:方法,传入YES为   偏移和方向参数。

此方法的行为将告诉hull定向到路径,并告诉它使用路径作为偏移量。即每次运行时它将移动50,100。

请改用此方法......

[SKAction followPath:path asOffset:NO orientToPath:YES duration:1.0];

这应该可以解决你的问题。

另外,摆脱那个序列。

这样做......

SKAction *moveAction = [SKAction followPath:path asOffset:NO orientToPath:YES duration:1.0];

[hull runAction:[SKAction repeatActionForever:moveAction]];
相关问题