我有一个简单的游戏场景,想在游戏结束后重设游戏。我将.sks用于GameScene。view.presentScene
确实运行良好,但是以下代码无法正常工作。
我该如何解决?
if let view = self.view {
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFill
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
let transition = SKTransition.fade(withDuration: 1.0)
view.presentScene(scene, transition: transition)
view.presentScene(scene, transition: SKTransition.crossFade(withDuration: 1.0)) //didn't work
//view.presentScene(scene) // work
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
更新
如果我使用过渡,则发生错误Fatal error: Unexpectedly found nil while unwrapping an Optional value
。
override func didMove(to view: SKView) {
self.backgroundColor = UIColor.blue
ball = self.childNode(withName: "Ball") as? SKSpriteNode
paddle = self.childNode(withName: "Paddle") as? SKSpriteNode
scoreLabel = self.childNode(withName: "Score") as? SKLabelNode
let urlPath = Bundle.main.url(forResource: "success", withExtension: "mp3")
do {
audioPlayer = try AVAudioPlayer(contentsOf: urlPath!)
audioPlayer.prepareToPlay()
} catch {
print("Error!")
}
ball.physicsBody?.applyImpulse(CGVector(dx: 50, dy: 50)) // error because ball is nil
let border = SKPhysicsBody(edgeLoopFrom: (view.scene?.frame)!)
border.friction = 1
self.physicsBody = border
self.physicsWorld.contactDelegate = self
}