Spritekit |斯威夫特3 | GameScene.sks - 物理属性仅适用于一个SKSpritenode

时间:2016-09-22 23:54:56

标签: xcode sprite-kit swift3

我有一个GameScene.sks,我添加了两个名为trampoline的SkSpritenodes。代码为trampoline对象提供物理体等,但在运行时。只有第一个蹦床才有身体。

知道为什么它不能应用于所有具有相同名称的节点吗?

//Trampoline
var trampoline: SKSpriteNode?       

 override func didMove(to view: SKView) {
trampoline = childNode(withName: "trampoline") as? SKSpriteNode
            trampoline?.physicsBody = SKPhysicsBody.init(rectangleOf: (trampoline?.size)!)
            trampoline?.physicsBody?.affectedByGravity = false
            trampoline?.physicsBody?.isDynamic = false
            trampoline?.physicsBody?.usesPreciseCollisionDetection = true
            trampoline?.physicsBody?.restitution = 1
}

1 个答案:

答案 0 :(得分:3)

childNode(withName: "trampoline") as? SKSpriteNode 

只会返回一个名为trampoline的节点,然后您将其分配给trampoline变量;你需要枚举所有称为trampoline的节点并设置它们的物理实体。 E.g。

enumerateChildNodesWithName("trampoline") { trampolineNode, _ in
    trampolineNode?.physicsBody = SKPhysicsBody.init(rectangleOf: (trampolineNode?.size)!)
    trampolineNode?.physicsBody?.affectedByGravity = false
    trampolineNode?.physicsBody?.isDynamic = false
    trampolineNode?.physicsBody?.usesPreciseCollisionDetection = true
    trampolineNode?.physicsBody?.restitution = 1
}

注意.isDynamic财产;如果'假',身体将不会参与碰撞或接触。