SKSpriteNode调整大小并更新SKPhysicsJointPin定位点

时间:2017-03-24 12:55:30

标签: ios objective-c swift sprite-kit

我试图学习SpriteKit。我有一个气球(SKSpriteNode),在运行SKAction进行调整大小后,气球将改变其大小。气球上有一根绳子。该字符串只有多个SKSpriteNodeSKPhysicsJointPin相互关联。我已经倒转了引力,所以带有附加弦的气球向上,3秒后超时它会变小。到目前为止它的工作原理除了字符串的起始附着点没有移动到气球的正确位置。如何解决这个问题?

enter image description here

所以我只在这里添加必要的部分,否则帖子太笨重了。所以再次看到气球有skaction调整气球的大小,以及如何在调整大小时将字符串移动到正确的位置?

Balloon是子类SKSpriteNode

 Balloon *balloon = [Balloon spriteNodeWithImageNamed:@"balloon.png"];
 [balloon setPosition:CGPointMake(200, CGRectGetMinY(scene.frame))];
 [balloon setName:@"balloon"];
 [balloon setUserInteractionEnabled:NO];
 [balloon setPhysicsBody:[SKPhysicsBody bodyWithCircleOfRadius:55]];
 balloon.physicsBody.dynamic = YES;
 [balloon.physicsBody setRestitution:0.4f];
 [balloon.physicsBody setFriction:0.0f];
 CGPoint attachPoint = CGPointMake(200, CGRectGetMinY(scene.frame));
 //create the rope
 SKRopeNode *rope = [SKRopeNode new];
 rope.name = @"ropeParent";
 [scene addChild:rope];
 [rope setAttachmentPoint:attachPoint toNode:balloon];   
 rope.ropeLength = 6;    
 [rope runAction:[SKAction sequence:@[[SKAction waitForDuration:4.5],    [SKAction removeFromParent]]]];   
 [balloon runAction:[SKAction sequence:@[[SKAction waitForDuration:4],   [SKAction scaleTo:balloon.yScale/2 duration:0.5], [SKAction removeFromParent]]]];

这是我如何创建用于创建长度的绳索方法

    SKSpriteNode *firstPart = [SKSpriteNode spriteNodeWithImageNamed:@"rope_part.png"];
    firstPart.position = _positionOnStartNode;
    firstPart.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:firstPart.size];
    firstPart.physicsBody.allowsRotation = NO;

    for (int i=1; i<ropeLength; i++) {
    SKSpriteNode *ropePart = [firstPart copy];
    ropePart.position = CGPointMake(firstPart.position.x, firstPart.position.y - (i*ropePart.size.height));
    ropePart.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ropePart.size];
    ropePart.physicsBody.allowsRotation = YES;
    [self.scene addChild:ropePart];
     }

这是为了在

之前添加绳索的关节
SKNode *nodeA = balloon;
SKSpriteNode *nodeB = [_ropeParts objectAtIndex:0];

SKPhysicsJointPin *joint = [SKPhysicsJointPin jointWithBodyA: nodeA.physicsBody
                                                       bodyB: nodeB.physicsBody
                                                      anchor: _positionOnStartNode];
    for (int i=1; i<_ropeParts.count; i++) {
    SKSpriteNode *nodeA = [_ropeParts objectAtIndex:i-1];
    SKSpriteNode *nodeB = [_ropeParts objectAtIndex:i];
    SKPhysicsJointPin *joint = [SKPhysicsJointPin jointWithBodyA: nodeA.physicsBody
                                                           bodyB: nodeB.physicsBody
                                                          anchor: CGPointMake(CGRectGetMidX(nodeA.frame),
                                                                              CGRectGetMinY(nodeA.frame))];
    [self.scene.physicsWorld addJoint:joint];
    }

0 个答案:

没有答案