我正在创建一个游戏,出现此错误:

0_specialized _fatalerrorMessage(StaticString,StaticString,StaticString,UInt,标志: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)
     }
    ]))
  }
}

最佳答案

我在重新创建问题时遇到了困难,因此我只为您重写了问题。这是一个简单的游戏...在主屏幕上点击六次即可获胜...然后它会在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可能会很快使您感到困惑。

在这里,我只是将所有内容隔开,赋予它自己的功能,因此您可以按照自己的意愿移动到更容易的位置。

关于ios - 0_specialized _fatalerrorMessage(StaticString,StaticString,StaticString,UInt,标志:UInt32)->从不-SpriteKit,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41575674/

10-10 20:01