2017-01-10 108 views
3

我正在创建一个游戏,以及我想要转换场景的位置。但是,使用过渡的场景时,我收到此错误:如何在SpriteKit中转换场景?

[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)     
     } 
     ])) 


----- 

The error:


Touching Variable

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 此错误。任何人都可以检查它。 PS:我正在让玩家/用户碰到一个粒子,当他们达到他们的极限时,我想过渡到恭喜的场景。任何人都可以检查我是否做得对吗?我相信这是崩溃。

这也是错误代码时崩溃:

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

+0

避免强行拆开可选项。否则,如果基础值为零,您可能会崩溃。此外,当使用初始化程序初始化颜色时,您应该提供0到1之间的范围内的RGBA分量,而不是0和255之间。 – Whirlwind

+0

您能告诉我一些代码,所以我可以理解吗? –

+0

我可以那样做。给我一秒钟打开我的电脑:) – Whirlwind

回答

0

移动整个代码重写FUNC didMove(查看:SKView)。它会工作。刚刚测试

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作为场景大小。此外,我评论了一条线,我使用无合并运算符(??),如果可选的基础值为零,它将提供默认值以供使用。或者如果你不想使用它,你可以使用可选的绑定语法(如果让...)。

+0

@Confused感谢困惑...错过了那一个。 – Whirlwind

+0

不用担心。我讨厌UI/SK颜色混乱的废话。如此长期而杂乱的创造一种颜色。 – Confused

+0

@Confused像这样的东西可能会工作:http://stackoverflow.com/a/27736004/3402095有点方便创建颜色。它可以让你做'let myColor = UIColor(r:255,g:255,b:255,a:255)' – Whirlwind