固定子节点不与其父节点一起移动

时间:2016-04-05 00:43:35

标签: sprite-kit swift2 skphysicsjoint

我发现了这个问题,听起来类似StackOverflow_24965533,但接受的答案在9.3中不起作用。

我所看到的例子: Link To Image Here

我的代码:

func addtopMachete() {

    let topMacheteTexture = SKTexture(imageNamed: "machete.png")
    topMacheteSwing = SKSpriteNode(texture: topMacheteTexture)
    topMacheteSwing.name = "topMacheteSwing_Node"
    topMacheteSwing.anchorPoint = CGPoint(x: 0.0, y: 0.0)

    let newX = topMacheteSwing.position.x
    let newY = topMacheteSwing.position.y + (hero.size.height / 2)
    let newLocation = CGPointMake(newX, newY)

    topMacheteSwing.position = newLocation
    topMacheteSwing.zRotation = -0.45

    let movetopMacheteSwing = SKAction.rotateToAngle(2.75, duration: 3)

    let removetopMacheteSwing = SKAction.removeFromParent()

    let moveAndRemovetopMacheteSwing = SKAction.sequence([movetopMacheteSwing, removetopMacheteSwing])

    topMacheteSwing.runAction(moveAndRemovetopMacheteSwing)

    topMacheteSwing.physicsBody = SKPhysicsBody(rectangleOfSize: topMacheteSwing.size, center: CGPointMake(topMacheteSwing.size.width / 2, topMacheteSwing.size.height / 2))

    topMacheteSwing.physicsBody!.dynamic = false
    topMacheteSwing.physicsBody!.affectedByGravity = false
    topMacheteSwing.physicsBody!.categoryBitMask = ColliderType.macheteContact.rawValue
    topMacheteSwing.physicsBody!.contactTestBitMask = ColliderType.monsterContact.rawValue | ColliderType.monster2HPContact.rawValue | ColliderType.riotShieldContact.rawValue
    topMacheteSwing.physicsBody!.usesPreciseCollisionDetection = true

    let topMacheteJoint = SKPhysicsJointFixed.jointWithBodyA(hero.physicsBody!, bodyB: topMacheteSwing.physicsBody!, anchor: CGPoint(x: CGRectGetMaxX(hero.frame), y: CGRectGetMaxY(hero.frame)))

    hero.addChild(topMacheteSwing)
    self.physicsWorld.addJoint(topMacheteJoint)
}

英雄测试用户界面只是灰色圆圈,当他被推动时,我希望子节点“topMacheteSwing”与他一起移动。我认为将它们固定在一起会做到这一点,但事实并非如此。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

因此,在完成多种可能的解决方案之后,我意识到答案不是使用固定关节而是简单地将砍刀固定在英雄身上而不是使用物理关节

topMacheteSwing.physicsBody!.pinned = true