我有以下SpriteKit代码:

scene.physicsWorld.gravity = CGVectorMake(0, -10);
...
- (void)update:(CFTimeInterval)currentTime
{
    [node.physicsBody applyForce:CGVectorMake(0, 10 * node.physicsBody.mass)];
}

为什么applyForce无法平衡重力?节点下降得很快。

完整示例:
@interface HelloScene()

@property SKLabelNode *node;

@end

@implementation HelloScene

- (void)didMoveToView:(SKView *)view
{
    self.backgroundColor = [SKColor blueColor];
    self.scaleMode = SKSceneScaleModeAspectFit;
    self.physicsWorld.gravity = CGVectorMake(0, -10);
    [self addChild:[self newHelloNode]];
}

- (SKNode *)newHelloNode
{
    self.node = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
    self.node.text = @"Hello, World!";
    self.node.fontSize = 42;
    self.node.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    self.node.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10.0];
    self.node.physicsBody.mass = 1.0;
    self.node.physicsBody.angularDamping = 0.0;
    self.node.physicsBody.linearDamping = 0.0;
    self.node.physicsBody.friction = 0.0;
    return self.node;
}

- (void)update:(NSTimeInterval)currentTime
{
    [self.node.physicsBody applyForce:CGVectorMake(0, 10 * self.node.physicsBody.mass)];
}

@end

编辑:

看起来好像是原来的150倍。这似乎起作用:
self.physicsWorld.gravity = CGVectorMake(0, -10.0/150.0);

为什么会这样呢?

最佳答案

@mitchellallison似乎已经进行了一些实验,发现这归因于像素与米的比率。参见applyForce(0, 400) - SpriteKit inconsistency

关于ios - SpriteKit重力困惑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20556549/

10-13 01:07