4

I am creating a game, and where I want to transition scenes. However, I am getting this error when using transitioning scenes:

[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

Does anyone know what is going on?

Here is my code:

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 This error . Can anyone check it.

    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: I'm making where player/user touches on a particle and when they reached their limit, I want to transition to the Congrats scene. Can anyone check if I did this right? I believe this is the crash.

This is also the error code when it crashes:

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

2 Answers2

2

This would be your GameOver class:

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

and a Congrats class:

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

}

About UIColor initializer...As I said, it accepts values from 0 to 1 rather than 0 to 255. Also you probably want to change values right now, because you are currently getting something similar to white color.

About forced unwrapping...I just skipped using that all, and used self.size for scene size. Also I commented a line where I use nil coalescing operator (??) which provides a default value to be used if optional's underlying value is nil. Or if you don't want to use that, you can use optional binding syntax (if let ...).

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • @Confused Thanks Confused...Missed that one. – Whirlwind Jan 10 '17 at 07:12
  • No worries. I frigging hate the messy nonsense of UI/SK color. So longwinded and messy to create a colour. – Confused Jan 10 '17 at 07:13
  • @Confused Something like this might work : http://stackoverflow.com/a/27736004/3402095 A bit convenient way to create colors. It lets you doing something like `let myColor = UIColor(r: 255 , g: 255, b: 255, a: 255)` – Whirlwind Jan 10 '17 at 07:15
  • 1
    Cheers. Better than waiting for Apple to realise Bret Victor might be onto something. – Confused Jan 10 '17 at 07:23
  • @SuzyHakobyan I tried this code and if you try it you will see that it work in an empty project. Anyway, you can set an exception break point and see which line causes the error. – Whirlwind Jan 10 '17 at 08:10
  • thank you anyways, I wil keep you update if anything happens. –  Jan 10 '17 at 08:14
  • 1
    @SuzyHakobyan No problem. Checkout this link to learn more about exception breakpoint : http://stackoverflow.com/a/17802942/3402095 – Whirlwind Jan 10 '17 at 08:15
  • Can you check my updated question. The Touching Variable. –  Jan 10 '17 at 17:40
0

Move the entire code to override func didMove(to view: SKView). It'll work. Just tested

Laxman Sahni
  • 572
  • 1
  • 6
  • 8