我正在尝试创建放入数组的节点,然后将它们与物理实体一起添加到场景中。
以下是创建初始精灵的代码:
let name = createTarget()
let targetNode = SKSpriteNode(imageNamed: name)
targetNode.name = name
chickenNodes.append(targetNode)
targetNode.position = generateRandomLocation()
let range = SKRange(lowerLimit: targetNode.position.y, upperLimit: targetNode.position.y)
let lockToCenter = SKConstraint.positionY(range)
targetNode.constraints = [lockToCenter]
if movingItems { animateTargets(targetNode) }
一旦所有这些节点都在数组中,我将它们在didMove中添加到场景中的背景节点fgNode,如下所示:
for chicken in chickenNodes {
let texture = SKTexture(imageNamed: chicken.name!)
chicken.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
chicken.physicsBody?.isDynamic = true
chicken.physicsBody?.affectedByGravity = true
chicken.physicsBody?.allowsRotation = false
chicken.physicsBody?.linearDamping = 0.0
chicken.physicsBody?.restitution = 1.0
chicken.physicsBody?.friction = 0.0
fgNode.addChild(chicken)
}
当我通过场景观察物理时,物理实体不断从精灵中掉落(好像它们对场景中的重力作出反应);精灵只是被锁定在原处。我如何确保physicsBody坚持精灵?