【问题标题】:How to transition scenes in SpriteKit?如何在 SpriteKit 中转换场景?
【发布时间】:2017-01-11 02:13:53
【问题描述】:

我正在创建一个游戏,并且我想在其中转换场景。但是,在使用过渡场景时出现此错误:

[图形] UIColor 使用远远超出预期范围的组件值创建。在 UIColorBreakForOutOfRangeColorComponents 上设置断点进行调试。此消息只会记录一次。 3致命错误:在展开可选值时意外发现 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))

        }

     }

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

    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) -> 从不

【问题讨论】:

  • 避免强行打开可选选项。否则,如果基础价值为零,您可能会崩溃。此外,当使用上面的初始化程序初始化颜色时,您应该提供介于 0 和 1 之间而不是介于 0 和 255 之间的 RGBA 分量。
  • 你能给我一些代码,让我看懂吗?
  • 我能做到。给我一点时间打开我的电脑:)
  • @SuzyHakobyan 你有暗行号是怎么回事?太酷了。
  • 你换了主题。

标签: swift sprite-kit


【解决方案1】:

这将是你的 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,则使用该值。或者如果你不想使用它,你可以使用可选的绑定语法(如果让 ...)。

【讨论】:

  • @Confused 谢谢 Confused...错过了那个。
  • 不用担心。我讨厌 UI/SK 颜色的乱七八糟的废话。如此冗长和凌乱,无法创造出一种颜色。
  • @Confused 这样的事情可能会起作用:stackoverflow.com/a/27736004/3402095 一种创建颜色的方便方法。它可以让你做类似let myColor = UIColor(r: 255 , g: 255, b: 255, a: 255)
  • 干杯。比等待 Apple 意识到 Bret Victor 可能会有所作为要好。
  • @SuzyHakobyan 没问题。查看此链接以了解有关异常断点的更多信息:stackoverflow.com/a/17802942/3402095
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多