如何从SKPhysicsJointSpring获得预期的行为?

时间:2014-03-31 03:16:24

标签: ios objective-c sprite-kit

我正在使用这个简单的场景在SpriteKit(即SKPhysicsJointSpring)中尝试弹簧关节。差不多,我有一个红色雪碧作为"天花板",然后是一个橙色精灵充当了一个群众"阻止"春天应该暂停它(注意:我没有绘制任何东西来连接两个方块,但只是想象那里有一个弹簧)。

enter image description here

使用默认的重力,我会期望橙色块会开始上下跳动,但事实上,它只是坐在那里。为了进一步让我感到困惑,如果我在场景-didMoveToView:方法的末尾取消注释一些力的应用,向量的x方向似乎实际上影响了橙色块(它开始充当钟摆),但y方向向量似乎不会影响任何事物。它好像弹簧真的像刚性杆一样。这应该发生吗?

最后,为什么钟摆式运动最终会减弱?似乎默认摩擦力是0.0,我自己没有施加任何摩擦力。有人可以帮助我更好地理解这个SKPhysicsJointSpring吗?

#import "XYZMainScene.h"

@interface XYZMainScene ()

@property (nonatomic, strong) SKSpriteNode *ceiling;
@property (nonatomic, strong) SKSpriteNode *block;

@end

@implementation XYZMainScene

- (void)didMoveToView:(SKView *)view {

    SKSpriteNode *ceiling = self.ceiling;
    [self addChild:ceiling];

    SKSpriteNode *block = self.block;
    [self addChild:block];

    SKPhysicsJointSpring *spring = [SKPhysicsJointSpring jointWithBodyA:ceiling.physicsBody
                                                                  bodyB:block.physicsBody
                                                                anchorA:ceiling.position
                                                                anchorB:block.position];
    [self.physicsWorld addJoint:spring];

//    [block.physicsBody applyForce:CGVectorMake(60, -100)];
}

- (SKSpriteNode *)ceiling {

    if (!_ceiling) {
        _ceiling = [SKSpriteNode spriteNodeWithColor:[SKColor redColor]
                                                size:CGSizeMake(30, 30)];

        _ceiling.position    = CGPointMake(self.frame.size.width/2, 400);
        _ceiling.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:_ceiling.position
                                                            toPoint:_ceiling.position];
    }
    return _ceiling;
}

- (SKSpriteNode *)block {

    if (!_block) {
        _block = [SKSpriteNode spriteNodeWithColor:[SKColor orangeColor]
                                              size:CGSizeMake(50,50)];

        _block.position    = CGPointMake(self.ceiling.position.x, self.ceiling.position.y - 200);
        _block.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_block.frame.size];
    }
    return _block;
}

@end

1 个答案:

答案 0 :(得分:2)

您需要更改frequency的{​​{1}}和damping属性。

SKPhysicsJointSpring

阅读SKPhysicsJointSpring类引用here