SpriteKit引力混乱

时间:2013-12-12 23:44:08

标签: ios sprite-kit

我有以下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);

为什么会出现这种情况?

1 个答案:

答案 0 :(得分:1)

看起来@mitchellallison做了一些实验,发现这是由于像素与米的比例。见applyForce(0, 400) - SpriteKit inconsistency