如何在SpriteKit中转换场景?

时间:2017-01-10 05:14:27

标签: swift sprite-kit

我正在创建一个游戏,我想要转换场景。但是,我在使用转换场景时遇到此错误:

  

[Graphics]使用远远超出预期范围的组件值创建的UIColor。在UIColorBreakForOutOfRangeColorComponents上设置断点以进行调试。此消息仅记录一次。 3fatal错误:在展开可选值时意外发现nil 2017-01-09 16:58:33.716407 MyGameApp [18371:5784169]致命错误:在解包可选值时意外发现nil

有谁知道发生了什么事?

这是我的代码:

import UIKit
import SpriteKit

class Congrats: 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() {
            let reveal = SKTransition.flipHorizontal(withDuration: 1.0)
            let scene = GameOver(size: (self.view?.frame.size)!)                
            self.view?.presentScene(scene, transition:reveal)                
        }
        ]))


-----
  

错误:   

  

触摸变量

  if countTouch > 10 {

      for touch: AnyObject in touches {
           let skView = self.view! as SKView
          skView.ignoresSiblingOrder = true
           var scene: Congrats!
          scene =  Congrats(size: skView.bounds.size)
           scene.scaleMode = .aspectFill
           skView.presentScene(scene, transition: SKTransition.doorsOpenHorizontal(withDuration: 1.0))

        }

     }

OR 这个错误。任何人都可以检查它。

    if firstTouch {
     shownTimer = Timer.scheduledTimer(timeInterval: 1, target: self,    selector: #selector(MyNewGame.decTimer), userInfo: nil, repeats: true)
     gameTimer = Timer.scheduledTimer(timeInterval: TIME_INCREMENT,  target:self, selector: Selector("endGame"), userInfo: nil, repeats: false)
      firstTouch = false
       }
PS:我正在制作玩家/用户触摸粒子的位置,当他们达到极限时,我想转换到Congrats场景。任何人都可以检查我是否做对了吗?我相信这是崩溃。

这也是崩溃时的错误代码:

  

0_specialized _fatalerrorMessage(StaticString,StaticString,StaticString,UInt,flags:UInt32) - >从来没有

2 个答案:

答案 0 :(得分:2)

这将是你的GameOver课程:

class GameOver:SKScene {

    override func didMove(to view: SKView) {

        backgroundColor = .purple
    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)

        if action(forKey: "transitioning") == nil{

            run(SKAction.sequence([
                SKAction.wait(forDuration: 1.0),
                SKAction.run() {[unowned self] in //now deinit will be called
                    let reveal = SKTransition.flipHorizontal(withDuration: 1.0)

                    //let scene = GameOver(size: (self.view?.frame.size) ?? CGSize(width: 335, height: 667))
                    let scene = Congrats(size: self.size)
                    self.view?.presentScene(scene, transition:reveal)
                }
                ]), withKey:"transitioning")


        }else{
            print("Transitioning in progress") //Just for a debug, you can remove this else statement
        }
    }

    deinit {
        print("GameOver scene deinitialized")
    }
}

和Congrats班:

class Congrats: SKScene {

    let label = SKLabelNode(fontNamed: "AppleSDGothicNeo-Bold")

    override func didMove(to view: SKView) {

        backgroundColor = UIColor(red: CGFloat(248.0 / 255.0), green: CGFloat(248.0 / 255.0), blue: CGFloat(248.0/255.0), alpha: 1.0)

        label.text = "Good Job! "
        label.fontSize = 22
        label.fontColor = SKColor.blue
        label.position = CGPoint(x: frame.midX, y: frame.midY)

        addChild(label)

    }

    deinit {
        print("Congrats scene deinitialized")
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)

        if action(forKey: "transitioning") == nil{

            run(SKAction.sequence([
                SKAction.wait(forDuration: 1.0),
                SKAction.run() {[unowned self] in //now deinit will be called.
                    let reveal = SKTransition.flipHorizontal(withDuration: 1.0)

                    //let scene = GameOver(size: (self.view?.frame.size) ?? CGSize(width: 335, height: 667))
                    let scene = GameOver(size: self.size)
                    self.view?.presentScene(scene, transition:reveal)
                }
                ]), withKey:"transitioning")


        }else{
            print("Transitioning in progress") //Just for a debug, you can remove this else statement
        }
    }

}

关于UIColor初始值设定项......正如我所说,它接受0到1而不是0到255的值。此外,您可能希望立即更改值,因为您当前正在获得类似于白色的值

关于强制解包...我只是跳过了使用全部,并使用self.size作为场景大小。我还评论了一条我使用nil coalescing运算符(??)的行,它提供了一个默认值,如果可选的基础值为nil则使用。或者,如果您不想使用它,您可以使用可选的绑定语法(如果让...)。

答案 1 :(得分:0)

移动整个代码以覆盖func didMove(查看:SKView)。它会工作的。刚刚测试过