How to connect two SKSpriteNode's using SKPhysicsJointPin - swift

Omen

I am trying to connect two SKSpriteNode's using SKPhysicsJointPin at anchorPoint marked as green dot on screenshot below. Later I would like enable physicsBody!.dynamic = true on object2 to get "swing animation" of object2.

enter image description here

enter image description here

I am having difficulty at the very beginning while creating the SKPhysicsJointPin even I don't get an error in Xcode it doesn't compile.

Here's the code:

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

let object1 = SKSpriteNode(imageNamed: "white")
let object2 = SKSpriteNode(imageNamed: "black")

override func didMoveToView(view: SKView) {
    // Setup background image
    self.backgroundColor = UIColor(hex: 0x60c0f3)

    // Setup physics body to the scene (borders)
    self.physicsBody = SKPhysicsBody (edgeLoopFromRect: self.frame)

    // Change gravity settings of the physics world
    self.physicsWorld.gravity = CGVectorMake(0, -9.8)
    self.physicsWorld.contactDelegate = self

    //===========================================

    // White object properties
    object1.physicsBody = SKPhysicsBody(rectangleOfSize: object1.frame.size)
    object1.physicsBody!.dynamic = false
    object1.position = CGPointMake(size.width/2 - object1.size.width/2, size.height/2)

    // Black object properties
    object2.physicsBody = SKPhysicsBody(rectangleOfSize: object2.frame.size)
    object2.physicsBody!.dynamic = true
    object1.anchorPoint = CGPointMake(0, 0)
    object2.position = CGPointMake(size.width/2 + object2.size.width + 2, size.height/2 + object2.size.height/2)

    // Create joint between two objects
    var myJoint = SKPhysicsJointPin.jointWithBodyA(object1.physicsBody, bodyB: object2.physicsBody, anchor: CGPoint(x: CGRectGetMaxX(self.object1.frame), y: CGRectGetMaxY(self.object2.frame)))

    self.physicsWorld.addJoint(myJoint)
    self.addChild(object1)
    self.addChild(object2)

}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}
}

The Xcode error I get is enter image description here

Please advise what is wrong in my code. Thank you.

0x141E

Two issues: 1) you need to add the nodes containing the physics bodies to the scene prior to connecting them with a joint and 2) you need to connect the nodes at the minimum Y value not the maximum (if you want the joint to behave as shown in your diagram), since the origin of the scene is the bottom/left corner of the view and positive Y is up.

    // Do this prior to adding the joint to the world
    self.addChild(object1)
    self.addChild(object2)

    // Create joint between two objects. Edit: changed MaxY to MinY to attach bodies 
    // at bottom of the nodes
    var myJoint = SKPhysicsJointPin.jointWithBodyA(object1.physicsBody, bodyB: object2.physicsBody, anchor: CGPoint(x: CGRectGetMaxX(self.object1.frame), y: CGRectGetMinY(self.object2.frame)))

    self.physicsWorld.addJoint(myJoint)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to pause an SKSpriteNode, Swift

Make SKSpriteNode subclass using Swift

How to declare SKSpriteNode globally in Swift

How to change a SKSpriteNode saturation? - Swift

Swift Sprite Kit recreating SKSpriteNode`s

Moving a SKSpriteNode in a downward loop, using Swift

How to seamlessly connect two LAN's?

How do I do consecutive function calls using the completion handler in Swift on an SKSpriteNode?

Using swift and spriteKit how can I rotate SKSpriteNode around the point outside of node?

How to instantiate SKSpriteNode spritekit swift 3

How to connect two points of two different plots using pyplot?

I need to connect two table's data using a trigger

Can't Connect two fpga's Zedboard using cisco switch

How to connect to a database using PHP's include

Swift SpriteKit Detect TouchesBegan on SKSpriteNode with SKPhysicsBody from Sprite's Texture

How to merge two queries using Firestore - Swift

Create parallax clouds with random Y coordinate using SKSpriteNode and SKAction in Swift

How do I connect two computers using USB 3.0?

How Do I connect two separate routes with a line using Leaflet?

How to connect two table using date and without any conditon?

How to connect two view controller to one view controller using Storyboard?

How Do I connect two coordinates with a line using Leaflet in R

How to connect two VM (postgreSQL and tomee) using Vagrant?

How to connect two QGraphicsItem by drawing line between them (using mouse)

How to connect points of two dataframes to each other using ggplot in R?

How to connect two sperate PCs using C# sockets

how can I connect two expressions using AND operator?

How to connect two google sheet workbooks using SUMIFS?

How to connect two Docker conteiners using "hostname" not the Internal IP