【问题标题】:How to connect two SKSpriteNode's using SKPhysicsJointPin - swift如何使用 SKPhysicsJointPin 连接两个 SKSpriteNode - swift
【发布时间】:2014-09-24 16:22:11
【问题描述】:

我正在尝试使用SKPhysicsJointPin 连接两个SKSpriteNodeanchorPoint 在下面的屏幕截图中标记为绿点。 稍后我想在object2上启用physicsBody!.dynamic = true以获得object2的“摆动动画”。

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

代码如下:

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 */
}
}

我得到的 Xcode 错误是

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

【问题讨论】:

    标签: swift sprite-kit xcode6 skspritenode skphysicsjoint


    【解决方案1】:

    两个问题: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)
    

    【讨论】:

      【解决方案2】:

      在示例代码中更新 SpriteKit API 调用。

      铰链的静止位置关闭,有时:

      1. 对角线应该垂直穿过重心(object2 有质量)
      2. 有时方块是垂直的(90 度)

      导入 SpriteKit 导入 SwiftUI

      class PinScene: SKScene {
          static let nodeSize = CGSize(width: 300, height: 50)
          let object1 = SKSpriteNode(color: .white, size: nodeSize)
          let object2 = SKSpriteNode(color: .red, size: nodeSize)
          
          override func didMove(to view: SKView) {
              print("PinScene size = \(size.width) x \(size.height)")
              self.anchorPoint = CGPoint(x: 0, y: 0) // BottomLeft
              
              // Create 1 of 2 physics bodies attach to a SKNode objects in the scene.
              object1.zPosition = 10
              object1.anchorPoint = CGPoint(x: 0, y: 0)
              object1.position = CGPoint(
                  x: size.width/2 - object1.size.width,
                  y: size.height/2
              )
              addChild(object1)
              object1.physicsBody = SKPhysicsBody(rectangleOf: PinScene.nodeSize)
              object1.physicsBody!.isDynamic = false
              
              // Create 2 of 2 physics bodies attach to a SKNode objects in the scene.
              object2.anchorPoint = CGPoint(x: 0, y: 0)
              object2.position = CGPoint(
                  x: size.width/2 + 1,
                  y: size.height/2
              )
              addChild(object2)
              object2.physicsBody = SKPhysicsBody(rectangleOf: PinScene.nodeSize)
              object2.physicsBody!.isDynamic = true
      
              // Create joint between two objects. Edit: changed MaxY to MinY to attach bodies
              // at bottom of the nodes
              let myJoint = SKPhysicsJointPin.joint(
                  withBodyA: object1.physicsBody!,
                  bodyB: object2.physicsBody!,
                  anchor: CGPoint(x: object1.frame.maxX, y: object1.frame.minY)
              )
      
              physicsWorld.add(myJoint)
          }
       
          override func update(_ currentTime: TimeInterval) {
              print("far end of Object2 at \(Int(object2.frame.maxX)), \(Int(object2.frame.minY)) ")
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-15
        • 2015-09-09
        • 2018-04-20
        • 2013-08-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多