问题
I am creating a game with parallax scroll and the player is animated with texture atlas. I have encountered various problems while trying to assign physicsBody to node with texture atlas initially. Anyway, now, while trying to run the game on the phone (iPhone X, iOS 13.5), everything works but when I try to run it on simulator (iOS13.5), it fails to create a physics body for the ground which is supposed to be created with the following function:
func parallaxScroll(image: String, y: CGFloat, z: CGFloat, duration: Double, needsPhysics: Bool) {
for i in 0 ... 1 {
// position the first node on the left, and position second on the right
let node = SKSpriteNode(imageNamed: image)
node.position = CGPoint(x: 1023 * CGFloat(i), y: y)
node.zPosition = z
addChild(node)
if needsPhysics {
_ = node.texture!.size()
node.physicsBody = SKPhysicsBody(texture: node.texture!, size: node.texture!.size())
if node.physicsBody == nil {
print("Failed to create physics body for ground")
} else {
print("Physics body for ground created successfully")
}
node.physicsBody?.isDynamic = false
node.physicsBody?.contactTestBitMask = 1
node.physicsBody?.collisionBitMask = 1
node.name = "ground"
}
// make this node move the width of the screen by whatever duration was passed in
let move = SKAction.moveBy(x: -1024, y: 0, duration: duration)
// make it jump back to the right edge
let wrap = SKAction.moveBy(x: 1024, y: 0, duration: 0)
// make these two as a sequence that loops forever
let sequence = SKAction.sequence([move, wrap])
let forever = SKAction.repeatForever(sequence)
// run the animations
node.run(forever)
}
}
Does anyone know what is the problem and how to solve it?
来源:https://stackoverflow.com/questions/62851919/could-not-create-physics-body-in-case-of-running-on-simulator