【问题标题】:Pinned child node not moving with its parent node固定子节点不随父节点移动
【发布时间】:2016-04-05 00:43:35
【问题描述】:

我发现了这个问题,听起来很像 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)
}

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

非常感谢您的帮助。

【问题讨论】:

    标签: sprite-kit swift2 skphysicsjoint


    【解决方案1】:

    所以在研究了多种可能的解决方案后,我意识到答案不是使用固定关节,而是简单地将砍刀固定在英雄身上,根本不使用物理关节

    topMacheteSwing.physicsBody!.pinned = true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多