SpriteKit 无法更改 SKPhysicsJointLimit maxLength

SpriteKit can't change SKPhysicsJointLimit maxLength

我有一个简单的项目,其中有两个 SKShapeNodes,每个 SKPhysicsBody。我有一个 SKPhysicsJointLimit 连接它们。虽然程序是 运行,但我需要更改 SKPhysicsJointLimit.maxLength 属性。但是,虽然值确实发生了变化,但屏幕上的物理特性并没有显示出来。

我是 运行 Xcode 6.4 正在部署到 iOS 7.0+

如何更改 maxLength 属性 以便更新关节的物理特性?

我这里有我的代码,它是从默认的 SpriteKit 项目修改而来的:

GameScene.h

#import <SpriteKit/SpriteKit.h>

@interface GameScene : SKScene

@property (strong) SKShapeNode *node1;
@property (strong) SKShapeNode *node2;
@property (strong) SKPhysicsJointLimit *limitJoint;
@property bool running;

@end

GameScene.m

#import "GameScene.h"

@implementation GameScene

-(void)didMoveToView:(SKView *)view {
    self.running = NO;

    // Create a simple square path for the nodes to use
    CGMutablePathRef square = CGPathCreateMutable();
    CGPathMoveToPoint(square, NULL, -10, 10);
    CGPathAddLineToPoint(square, NULL, -10, -10);
    CGPathAddLineToPoint(square, NULL, 10, -10);
    CGPathAddLineToPoint(square, NULL, 10, 10);
    CGPathAddLineToPoint(square, NULL, -10, 10);
    CGPathCloseSubpath(square);

    // Create node 1
    self.node1 = [SKShapeNode node];
    self.node1.position = CGPointMake(450, 512);
    self.node1.path = square;
    self.node1.fillColor = [SKColor blackColor];
    // I know that I'm using a circular physics body here, but for me a circle is fine
    self.node1.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
    // Will turn it on after the user taps the screen, so that we can watch it run
    self.node1.physicsBody.dynamic = NO;
    [self.scene addChild:self.node1];

    // Create node 2
    self.node2 = [SKShapeNode node];
    self.node2.position = CGPointMake(550, 512);
    self.node2.path = square;
    self.node2.fillColor = [SKColor blackColor];
    self.node2.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
    // Node 2 will always stay in place
    self.node2.physicsBody.dynamic = NO;
    [self.scene addChild:self.node2];

    // Create the limit joint between the two nodes
    self.limitJoint = [SKPhysicsJointLimit jointWithBodyA:self.node1.physicsBody bodyB:self.node2.physicsBody anchorA:self.node1.position anchorB:self.node2.position];
    [self.scene.physicsWorld addJoint:self.limitJoint];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (self.running) {
        // Set the new maxLength
        float newMaxLength = self.limitJoint.maxLength + 50.0;
        [self.limitJoint setMaxLength:newMaxLength];
        NSLog(@"New length: %f", self.limitJoint.maxLength);
    } else {
        // Turn on the physics for node 1
        self.running = YES;
        self.node1.physicsBody.dynamic = YES;
    }
}

-(void)update:(CFTimeInterval)currentTime {
    // Nothing here
}

@end

这段代码的输出是:

2015-08-18 19:55:33.494 LimitTest[6925:60b] New length: 150.000015
2015-08-18 19:55:33.706 LimitTest[6925:60b] New length: 200.000031
2015-08-18 19:55:33.897 LimitTest[6925:60b] New length: 250.000031
2015-08-18 19:55:34.134 LimitTest[6925:60b] New length: 300.000031
2015-08-18 19:55:34.381 LimitTest[6925:60b] New length: 350.000031
2015-08-18 19:55:34.614 LimitTest[6925:60b] New length: 400.000061
2015-08-18 19:55:35.784 LimitTest[6925:60b] New length: 450.000061
2015-08-18 19:55:36.136 LimitTest[6925:60b] New length: 500.000061
2015-08-18 19:55:36.401 LimitTest[6925:60b] New length: 550.000061
2015-08-18 19:55:36.630 LimitTest[6925:60b] New length: 600.000061
2015-08-18 19:55:36.890 LimitTest[6925:60b] New length: 650.000122
2015-08-18 19:55:37.130 LimitTest[6925:60b] New length: 700.000122
2015-08-18 19:55:37.368 LimitTest[6925:60b] New length: 750.000122
2015-08-18 19:55:37.611 LimitTest[6925:60b] New length: 800.000183
2015-08-18 19:55:37.846 LimitTest[6925:60b] New length: 850.000183
2015-08-18 19:55:38.102 LimitTest[6925:60b] New length: 900.000244
2015-08-18 19:55:38.313 LimitTest[6925:60b] New length: 950.000244
2015-08-18 19:55:38.568 LimitTest[6925:60b] New length: 1000.000244

我很确定真正小的十进制值只是一些不准确,我并不担心。我只是表明 maxLength 属性 确实发生了变化。

我看过this question here,这是一年多前的问题,但没有解决方案。在评论中,他说他找到了一个解决方案,他对修复的简短描述是关于在返回对象之前进行更改。但据我所知,这不适用于我。有人要求他澄清,但似乎被忽略了。

0x141E 提供了有关如何执行此操作的信息,建立了我无法建立的连接。基本上在移除第一个关节后创建一个新关节。我的关节有一个全局变量,这对我来说效果很好。这是我现在在 touchesBegan

中使用的代码
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (self.running) {
        // Create the new maxLength
        float newMaxLength = self.limitJoint.maxLength + 50.0;
        [self.scene.physicsWorld removeJoint:self.limitJoint];

        self.limitJoint = [SKPhysicsJointLimit jointWithBodyA:self.node1.physicsBody bodyB:self.node2.physicsBody anchorA:self.node1.position anchorB:self.node2.position];
        [self.limitJoint setMaxLength:newMaxLength];

        [self.scene.physicsWorld addJoint:self.limitJoint];
        NSLog(@"New length: %f", self.limitJoint.maxLength);
    } else {
        self.running = YES;
        self.node1.physicsBody.dynamic = YES;
    }
}