更改具有physicsBody的SKSpriteNode的位置

Nor*_*man 12 objective-c ios sprite-kit

我无法通过更改来更改SKSpriteNode的位置

self.position = newPosition;
Run Code Online (Sandbox Code Playgroud)

如果它有物理实体,它就不再起作用了.

我得到的解决方法是:

    self.myStar.physicsBody = nil;

    [self.myStar changePosition];

    self.myStar.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.myStar.size.width/2];
Run Code Online (Sandbox Code Playgroud)

要么

    [self runAction:[SKAction moveTo:newPosiion duration:0.0]];
Run Code Online (Sandbox Code Playgroud)

但#2并不顺利.它需要在没有移动动作的情况下弹出另一个位置.

小智 10

I had the same problem. Apparently you cannot explicitly set the position of a Sprite node once it has a PhysicsBody. I solved it by temporarily removing the sprite node's PhysicsBody.

CGFloat yPosition = 400.f;
SKPhysicsBody* goldfishPhysicsBody = _goldfish.physicsBody;
_goldfish.physicsBody = nil;
_goldfish.position = CGPointMake(_goldfish.position.x, yPosition);
_goldfish.physicsBody = goldfishPhysicsBody;
Run Code Online (Sandbox Code Playgroud)


pro*_*cal 3

我认为你遇到的问题是,为了让你控制物理身体,你需要将其设置为动态。

self.myStar.physicsBody.dynamic = NO;

如果该dynamic属性设置为YES,则物理引擎将控制其运动。

正如评论中所指出的,设置dynamicNO不应限制通过 SKAction 或position属性的移动。但是,如果将其设置为YES,则物理引擎中的某些内容可能会影响您移动节点的尝试。

如果您希望运动不弹出,那么您需要将持续时间设置为大于零SKAction,否则它会像您所描述的那样弹出。将持续时间设置为 0.0 与仅更改位置相同。

例如 :

[self runAction:[SKAction moveTo:newPosition duration:1.0]];

将在 1 秒内将节点移动到新位置。

  • 关于动态是/否的部分是错误的。您始终可以通过操作或设置 node.position 来更改主体的位置,无论是否动态。 (3认同)