如何使用SKPhysicsJointPin连接两个SKSpriteNode - swift

时间:2023-01-23 07:44:24

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.

我试图在下面的截图标记为绿点的anchorPoint上使用SKPhysicsJointPin连接两个SKSpriteNode。后来我想在object2上启用physicsBody!.dynamic = true来获取object2的“swing动画”。

如何使用SKPhysicsJointPin连接两个SKSpriteNode  -  swift

如何使用SKPhysicsJointPin连接两个SKSpriteNode  -  swift

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.

我在创建SKPhysicsJointPin时一开始遇到困难,即使我没有在Xcode中得到错误也无法编译。

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 如何使用SKPhysicsJointPin连接两个SKSpriteNode  -  swift

我得到的Xcode错误是

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

请告知我的代码有什么问题。谢谢。

1 个解决方案

#1


3  

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.

两个问题:1)您需要在将节点连接到关节之前将包含物理主体的节点添加到场景中2)您需要以最小Y值而不是最大值连接节点(如果您希望关节表现出来)如图所示),因为场景的原点是视图的左下角,而正Y是向上的。

    // 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)

#1


3  

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.

两个问题:1)您需要在将节点连接到关节之前将包含物理主体的节点添加到场景中2)您需要以最小Y值而不是最大值连接节点(如果您希望关节表现出来)如图所示),因为场景的原点是视图的左下角,而正Y是向上的。

    // 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)