0

I have two SKShapeNode nodes that I want to be attached to each other. When I tap to perform an action I want the two nodes to expand their height and upon tapping again I want them to retract. The problem is that the two nodes aren't animating as expected:

Before animation: (unexpanded) enter image description here

After animation: (expanded) enter image description here

Both nodes are expanding but it appears that one is going under the other. What I would want to happen is that they "push" each other away.

override func didMoveToView(view: SKView) {
    let size = CGSize(width: view.frame.width, height: 100)

    let firstSection = SKShapeNode(rectOfSize: size)
    firstSection.physicsBody = SKPhysicsBody(rectangleOfSize: size)
    firstSection.fillColor = SKColor.purpleColor()
    firstSection.physicsBody?.affectedByGravity = false
    firstSection.position = CGPoint(x: 500, y: 400)
    firstSection.name = "section"
    self.addChild(firstSection)




    let secondSection = SKShapeNode(rectOfSize: size)
    secondSection.fillColor = SKColor.blueColor()
    secondSection.physicsBody = SKPhysicsBody(rectangleOfSize: size)
    secondSection.physicsBody?.affectedByGravity = false
    secondSection.position = CGPoint(x: 500, y: 300)
    secondSection.name = "section"
    self.addChild(secondSection)

    let joint = SKPhysicsJointSpring.jointWithBodyA(firstSection.physicsBody, bodyB: secondSection.physicsBody, anchorA: firstSection.position, anchorB: secondSection.position)

    self.physicsWorld.addJoint(joint)
}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
  • I think you need to set up the physicsWorld for the view. – Kendel Jan 20 '15 at 03:48
  • I thought there was a default physicsWorld on the scene? If I don't have the joint the objects push each other away as expected but don't contract back due to no joint – Kyle Decot Jan 20 '15 at 13:33
  • changing the size of a node won't necessarily change it's physicsBody. In your GameViewController if you set skView.showsPhysics=true then you can see this. –  Jan 20 '15 at 16:53

1 Answers1

0

Okapi's comment is correct. Changing the path of an SKShapeNode does not change the size of the attached physics body. However changing the scale does.

The solution would be to use

SKAction.scaleYTo(endScale, duration: time)

Okapi's also correct in that if you'd like to see what's actually happening in your physics simulation you should set the SKViews showsPhysics property to true.

Hope that's helpful.

EDIT: I just realized you're using SKShapeNode, not SKSpriteNode. The scaling physics body still applies, but I'm not sure how you're resizing your nodes (scaling/resizing the path?)

Nightly
  • 581
  • 3
  • 10