我正在创建一个游戏,我收到此错误:
0_specialized _fatalerrorMessage(StaticString,StaticString,StaticString,UInt,flags:UInt32) - >从未
这里是我认为崩溃的代码:
func goToCongratsScene() {
if countTouches > 5 {
let gameScene:GameScene = GameScene(size: self.size)
let transition = SKTransition.reveal(with: SKTransitionDirection.down, duration: 0)
gameScene.scaleMode = SKSceneScaleMode.aspectFill
self.view!.presentScene(gameScene, transition: transition)
}
}
游戏场景
import UIKit
import SpriteKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
backgroundColor = UIColor(red: CGFloat(248), green: CGFloat(248), blue: CGFloat(248), alpha: CGFloat(255)) //SKColor
var message = "Good Job! "
let label = SKLabelNode(fontNamed: "AppleSDGothicNeo-Bold")
label.text = message
label.fontSize = 22
label.fontColor = SKColor.blue
self.backgroundColor = SKColor.black
label.position = CGPoint(x: size.width / 2, y: size.height / 2)
addChild(label)
run(SKAction.sequence([
SKAction.wait(forDuration: 1.0),
SKAction.run(){
var scene = GameOver(size: self.size)
let skView = self.view! as SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .resizeFill
scene.size = skView.bounds.size
skView.presentScene(scene)
}
]))
}
}
答案 0 :(得分:1)
我很难在问题中重新创建你的问题,所以我只是为你重写了它。这是一个简单的游戏......你在主屏幕上点击六次赢得...然后它会在1.5秒(你的延迟)后自动失败
在游戏结束屏幕上点按即可重置。如果您在主屏幕上拖动而不是点击,则会立即失败。
如果你需要,我可以愉快地解释一下。希望它有所帮助:
import SpriteKit
// Give us some functions that we can use in all of our scenes (inheritance):
extension SKScene {
// We don't need to use `self` anywhere for now,
// because `size` and `view` are are already members of `SKScene`.
func goToGameOverScene() {
let scene = GameOver(size: size)
scene.scaleMode = .resizeFill
view!.ignoresSiblingOrder = true
view!.presentScene(scene)
}
func goToCongratsScene() {
let congratsScene = Congrats(size: size)
let transition = SKTransition.reveal(with: SKTransitionDirection.down,
duration: 0)
congratsScene.scaleMode = .aspectFill
view!.presentScene(congratsScene, transition: transition)
}
func goToGameScene() {
let gameScene = GameScene(size: size)
gameScene.scaleMode = .aspectFill
view!.presentScene(gameScene)
}
func makeLabel(text: String) {
let label = SKLabelNode(fontNamed: "AppleSDGothicNeo-Bold")
label.text = text
label.fontSize = 22
label.fontColor = SKColor.blue
label.position = CGPoint(x: size.width / 2, y: size.height / 2)
addChild(label)
}
}
class GameOver: SKScene {
override func didMove(to view: SKView) {
makeLabel(text: "Game Over!! Muahahaha!")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
goToGameScene()
}
}
class Congrats: SKScene {
override func didMove(to view: SKView) {
backgroundColor = SKColor(red: CGFloat(248),
green: CGFloat(248),
blue: CGFloat(248),
alpha: CGFloat(255))
backgroundColor = SKColor.black // Not sure which color you wanted :)
makeLabel(text: "you won... or did you?")
run(.sequence([
.wait(forDuration: 1.5),
.run() { self.goToGameOverScene() } // We have to use `self` here because in closure.
]))
}
}
class GameScene: SKScene {
var countTouches = 0
override func didMove(to view: SKView) {
makeLabel(text: "Tap six times to 'win'~!")
}
// Six taps to win!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
countTouches += 1
if countTouches > 5 { goToCongratsScene() }
}
// Game over if you move your finger!
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
goToGameOverScene()
}
}
我认为您的问题来自尝试交换didMoveToView
内部的场景和闭包......并且使用self
可能会很快混淆。
在这里,我只是将所有内容分开,给它自己的功能,所以你可以跟随哪里移动到我认为更容易的地方。