子节点在添加到父节点时不保持相同的physicsBody

时间:2016-06-22 17:21:52

标签: sprite-kit

使用Xcode SpriteKit场景编辑器我将一些对象添加到场景中作为“障碍物”。我喜欢在屏幕上制作动画。它们不是动态的。为了简化操作,我想将它们添加到一个obstacleParent节点,然后只为该一个对象设置动画。问题是为父母添加障碍完全搞砸了他们的物理机构。意味着障碍物理实体与精灵的轮廓不匹配。

1 个答案:

答案 0 :(得分:1)

我不确定它是否是场景编辑器中的错误,但是以编程方式添加物理主体会产生预期的结果。

值得注意的一些有趣的事情是:

物理实体与父母一起移动,无论它们是否不是动态的。

我无法让它们相互重叠,直到我让它们都具有相同的contactTestBitMask

在屏幕截图中很难分辨,但所有3个孩子都保持了物理形状

如果父亲有一个物理身体并且物理冲动被应用于它,则只有父亲移动。但是,以编程方式设置父级的位置会移动父节点和子节点enter image description here

    testObject = SKSpriteNode(texture: nil, color: .greenColor(), size: CGSize(width: 100, height: 100))
    testObject.zPosition = Layer.Controls.rawValue
    testObject.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
//        testObject.physicsBody = SKPhysicsBody(circleOfRadius: testObject.width / 2)
//        testObject.physicsBody!.dynamic = true
//        testObject.physicsBody!.affectedByGravity = false
//        testObject.physicsBody!.allowsRotation = true
    addChild(testObject)

    let testerObject = SKSpriteNode(texture: nil, color: .clearColor(), size: CGSize(width: 100, height: 100))
    testerObject.zPosition = Layer.Controls.rawValue
    testerObject.position = CGPoint(x:50, y: 50)

    testerObject.physicsBody = SKPhysicsBody(circleOfRadius: testerObject.width / 2)
    testerObject.physicsBody!.affectedByGravity = false
    testerObject.physicsBody!.dynamic = true
    testerObject.physicsBody!.allowsRotation = true
    testerObject.physicsBody!.collisionBitMask = 0
    testerObject.physicsBody!.contactTestBitMask = 0

    testObject.addChild(testerObject)

    let testerObject2 = SKSpriteNode(texture: nil, color: .clearColor(), size: CGSize(width: 100, height: 100))
    testerObject2.zPosition = Layer.Controls.rawValue
    testerObject2.position = CGPoint(x:-50, y: -50)

    testerObject2.physicsBody = SKPhysicsBody(circleOfRadius: testerObject.width / 2)
    testerObject2.physicsBody!.affectedByGravity = false
    testerObject2.physicsBody!.dynamic = true
    testerObject2.physicsBody!.allowsRotation = true
    testerObject2.physicsBody!.collisionBitMask = 0
    testerObject2.physicsBody!.contactTestBitMask = 0
    testObject.addChild(testerObject2)

    let testerObject3 = SKSpriteNode(texture: nil, color: .clearColor(), size: CGSize(width: 100, height: 100))
    testerObject3.zPosition = Layer.Controls.rawValue
    testerObject3.physicsBody = SKPhysicsBody(circleOfRadius: testerObject.width / 2)
    testerObject3.physicsBody!.affectedByGravity = false
    testerObject3.physicsBody!.dynamic = true
    testerObject3.physicsBody!.allowsRotation = true
    testerObject3.physicsBody!.collisionBitMask = 0
    testerObject3.physicsBody!.contactTestBitMask = 0
    testObject.addChild(testerObject3)