我有一个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
}
答案 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
财产;如果'假',身体将不会参与碰撞或接触。