在 Sprite 工具包级别编辑器中创建的场景不起作用 [英] Scene created in Sprite kit level editor is not working

查看:14
本文介绍了在 Sprite 工具包级别编辑器中创建的场景不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这样做一段时间.我有一个名为 PlayScene 的游戏的主要场景.我那里有一个暂停按钮.当玩家点击该按钮时,我想加载另一个名为 PauseScene 的场景.为了直观地创建该场景,我使用 Sprite 工具包级别编辑器.所以我有两个文件 PauseScene.swiftPauseScene.sks.但是 .sks 内容(现在只是一个背景)并没有取消归档.我真的不知道我会在哪里犯错.所以这是我通过暂停按钮的过渡,位于 PlayScene

I'm trying to do this for a while. I have a main scene for a game named PlayScene. I have a pause button there. When player is tapping that button I want to load another scene, named PauseScene. For visualy creating that scene I use Sprite kit level editor. So I have two files PauseScene.swiftand PauseScene.sks. But .sks content ( just a background for now) isn't unarchiving. I don't really know where could I make a mistake. So this is my transition via pause button, located in PlayScene

for touch: AnyObject in touches {
    let location = touch.locationInNode(self)
        if self.nodeAtPoint(location) == self.pause {
            var scene = PauseScene(size: self.size)
            scene.paused = true
            let skView = self.view as SKView!
            skView.ignoresSiblingOrder = true
            scene.scaleMode = .AspectFill
            scene.size = skView.bounds.size
            skView.presentScene(scene)
        }
    }    

这就是我在 PauseScene.swift 中的内容:

And this is what I have in PauseScene.swift:

class PauseScene: SKScene {
override func didMoveToView(view: SKView) {
    self.initPauseScene()
}
func initPauseScene() {

}

}

我试过 this 并不能很好地工作.但是 该教程 是我能解决的最接近的问题.它没有崩溃,也没有显示任何错误,但是当我点击暂停按钮时,它会过渡到没有我背景的场景,只是标准的灰色场景.这个 答案对我不起作用.第一个建议显示错误,当我尝试第二个并点击暂停时 - 游戏崩溃了.我不知道,那个关卡编辑器是为了更轻松的工作而设计的,但要知道它对我来说只是加倍.我什至认为问题可能出在我的背景图像中.但它的标准.png.为什么我的内容没有显示?所以我试图复制 GameViewController 默认代码来加载 GameScene 并最终崩溃.这不是我第一次在尝试取消存档时遇到 那个错误.这是什么意思?当我尝试用 if let scene = PauseScene.unarchiveFromFile("PauseScene") as 替换 var scene = PauseScene(size: self.size) 时,我得到了这个?暂停场景

I tried this and it's not working very well. However that tutorial is closest I could get with my problem. It's not crushing and isn't showing any errors, but when I tap pause button it's transitioning to that scene without my background, just standard grey scene. This answer is not working for me. First suggestion show errors and when I tried second and tap the pause – game is crushing. I don't know, that level editor is made for easier work but for know it just doubles it for me. I even thought that problem is in my background image maybe. But its standard .png. Why my content isn't showing up? So I tried to copy GameViewController default code for loading GameScene and ended up with crashing. It's not the first time I have that error while trying to unarchive this. What does it mean? I got this when i tried to replace var scene = PauseScene(size: self.size) with a if let scene = PauseScene.unarchiveFromFile("PauseScene") as? PauseScene

推荐答案

可能由于拼写错误或文件不在mainBundle中而过渡到灰色场景.我已经对此进行了测试,并且可以正常工作.您需要确保在加载时写入 .sks 文件的确切名称,否则会出现灰屏.

It may be transition to a grey scene due to spelling errors or the file not even being in the mainBundle. I have tested this and it works. You need to make sure that you are writing the exact name of the .sks file when loading or else you get a grey screen.

如果文件名为 GameScreen.sks 并且类名为 GameScene 那么如果在加载 .sks 文件时输入类名,则会出现灰屏.

If the file is called GameScreen.sks and the class is called GameScene then if you put the class name when loading the .sks file you will get a grey screen.

let doors = SKTransition.doorwayWithDuration(1.0)
let archeryScene = GameScene(fileNamed: "GameScreen")
self.view?.presentScene(archeryScene, transition: doors)

因此,如果我们查看第二行代码,我们会看到它正在加载名称为 GameScreen 的 .sks 文件,而不是类名称 GameScene.我们也中止使用文件扩展名,因为编译器默认知道它是我们在 mainBundle 中搜索的 .sks 文件.

So if we look at the second line of code, We see that it is loading the .sks file with the name GameScreen opposed to the class name GameScene. We also abort using the file extension because the compiler know by default it is a .sks file we are searching for in the mainBundle.

如果你老是灰屏,那可能是你GameViewController拼写错误,在SKNode Extension中,一定要改

If you do keep getting a grey screen, Then it may be an error in spelling or ing you GameViewController, In the SKNode Extension, Be sure to change

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 GameScene
        archiver.finishDecoding()
        return scene
    } else {
        return nil
    }
}
}

到这个..

 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 SKScene
        archiver.finishDecoding()
        return scene
    } else {
        return nil
    }
   }
}

我们刚刚编辑了这行代码,

We just edited this line of code,

 let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene

到这里

 let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as SKScene

这将允许我们加载多个 .sks 文件而不会出现任何错误或崩溃.

This will allow us to load multiple .sks files without any errors or crashes.

这篇关于在 Sprite 工具包级别编辑器中创建的场景不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆