【问题标题】:Swift spritekit change scenes?Swift spritekit 改变场景?
【发布时间】:2015-01-25 01:30:10
【问题描述】:

我有一个使用 SpriteKit 的 swift 游戏。我做了一个游戏,但现在我想创建一个菜单场景。所以我改变了 GameViewController 来加载 MenuScene.swift 像这样

extension SKNode {
   class func unarchiveFromFile(file : NSString) -> SKNode? {
    if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
        var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
        var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
        let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as MenuScene
        archiver.finishDecoding()
        return scene
    } else {
        return nil
    }
  }


}

class GameViewController: UIViewController {



override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = MenuScene.unarchiveFromFile("MenuScene") as? MenuScene {
    //if scene = MenuScene.unarchiveFromFile("MenuScene") as? MenuScene {
        // Configure the view.
        let skView = self.view as SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        scene.size = skView.bounds.size

        skView.presentScene(scene)
    }
}

它可以很好地加载 MenuScene,我创建了一个播放按钮,但现在我想在点击时转换到游戏场景,所以我输入了这段代码

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)

        if(play.containsPoint(location)) {



            gamescene = GameScene(size: self.scene!.size)
            self.view!.presentScene(gamescene, transition:reveal)

        }
     }
   }

我尝试了许多其他尝试,但每次它都崩溃到 AppDelegate.swift 并出现错误:线程 1:EXC_BAD_ACCESS(code=EXC_I386_GPFLT)

那我做错了什么?

【问题讨论】:

  • 尝试用self.size替换self.scene!.size
  • 崩溃发生在哪一行?
  • 在 AppDelegate.swift 的这一行:class AppDelegate: UIResponder, UIApplicationDelegate {
  • 尝试用self.frame.size替换self.scene!.size

标签: swift sprite-kit


【解决方案1】:

问题出在 sknode 扩展部分。

extension SKNode {
   class func unarchiveFromFile(file : NSString) -> SKNode? {
    if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
        var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
        var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")

/*
Below Line is the problem. you create an object with gameScene object 
    type However in here you call it as MenuScene. 
    */
        let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as MenuScene
        archiver.finishDecoding()
        return scene
 } else {
        return nil
 }
}
}

它将通过创建新的 skNode 扩展并对其进行修改来修复。比如:

extension SKNode {
   class func unarchiveFromFile2(file : NSString) -> SKNode? {
    if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
        var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
        var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene") 
        let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as gameScene
        archiver.finishDecoding()
        return scene
   } else {
        return nil
   }

} }

它的答案很晚,但必须有解决方案..

【讨论】:

    【解决方案2】:

    使用此代码更改您的场景:

    func changeScene(){
        let scene =  NameScene(size: self.view!.bounds.size)
        scene.backgroundColor = SKColor(red: 1, green: 1, blue: 1, alpha: 1)
        scene.scaleMode = .AspectFill
        scene.backgroundColor = UIColor.whiteColor()
        let transition = SKTransition.pushWithDirection(SKTransitionDirection.Left, duration:0.5)
        self.scene?.view?.presentScene(scene, transition: transition)
    
    }
    

    【讨论】:

      【解决方案3】:

      原来我在菜单场景中有一个粒子发射器。我在转换时忘记将其删除。所以它试图发射到一个不再存在的场景。修复它就像在过渡到游戏场景之前添加这条线一样简单。

      myparticle.removeFromParent();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多