如何在SpriteKit中切换场景?

4
我正在制作一个游戏,想要实现场景过渡。然而,在使用场景过渡时出现了以下错误:
[Graphics] UIColor created with component values far outside the expected range. Set a breakpoint on UIColorBreakForOutOfRangeColorComponents to debug. This message will only be logged once. 3fatal error: unexpectedly found nil while unwrapping an Optional value 2017-01-09 16:58:33.716407 MyGameApp[18371:5784169] fatal error: unexpectedly found nil while unwrapping an Optional value 有人知道是怎么回事吗?
这是我的代码:
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: 我正在制作一个玩家/用户触摸粒子的游戏,当他们达到限制时,我想过渡到祝贺场景。有人可以检查我是否做对了吗?我认为这是崩溃的原因。

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

0_specialized _fatalerrorMessage(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> Never


避免强制解包可选项,否则如果底层值为nil,则可能会崩溃。此外,在使用上述初始化程序初始化颜色时,应在0到1的范围内提供RGBA组件,而不是在0到255之间。 - Whirlwind
你能给我展示一些代码,这样我就能理解了吗? - user7138913
我可以做到。给我一秒钟打开电脑 :) - Whirlwind
@SuzyHakobyan 你是怎么让行号变成暗色的?太酷了。 - Confused
1
你更改主题。 - user7138913
2个回答

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")
    }
}

一个祝贺类:
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合并运算符(??),该运算符提供要在可选的基础值为nil时使用的默认值。或者,如果您不想使用它,可以使用可选绑定语法(if let ...)。

@Confused 谢谢你,我有点糊涂了...漏掉了那个。 - Whirlwind
1
比起等待苹果意识到 Bret Victor 的想法,这真是太好了。 - Confused
@SuzyHakobyan 我试过这段代码,如果你也试一下,就会发现它在一个空项目中可以正常工作。无论如何,你可以设置异常断点,看看是哪一行导致了错误。 - Whirlwind
无论如何,谢谢你。如果有任何情况发生,我会让你知道的。 - user7138913
1
@SuzyHakobyan 没问题。请查看此链接以了解有关异常断点的更多信息:https://dev59.com/m2Mm5IYBdhLWcg3wMssp#17802942 - Whirlwind
显示剩余3条评论

0

将整个代码移动到覆盖函数didMove(to view:SKView)中。它会起作用的。刚刚测试过


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接