如何将 .sks 文件链接到 SpriteKit 中的 .swift 文件

编程入门 行业动态 更新时间:2024-10-28 06:27:30
本文介绍了如何将 .sks 文件链接到 SpriteKit 中的 .swift 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我看到在 XCode 6 中有一个内置的关卡编辑器.我找不到很多关于如何使用它或它如何工作的文档,但我能够弄清楚如何使用它来制作 SKSpriteNodes,然后使用 childNodeWithName() 将它们链接到 .swift 文件.当你创建一个新的 SpriteKit 项目时,它从一个 GameScene.swift 和一个 GameScene.sks 开始.我能够使用这些来视觉化地制作一个关卡,然后我可以在 GameScene.swift 中为它编码,但是当我尝试为一个新关卡制作我自己的 .sks 和 .swift 文件时,我无法连接 .sks和我制作的 .swift 文件.我尝试完全按照本教程进行操作:

I saw that in XCode 6 there is a built in level editor. I could not find a lot of documentation on how to use it or how it works, but I was able to figure out how to use it to make SKSpriteNodes then link them to a .swift file using childNodeWithName(). When you make a new SpriteKit project, it starts with a GameScene.swift and a GameScene.sks. I was able to use these to make one level visually, then I could code for it in the GameScene.swift, but when I tried to make my own .sks and .swift file for a new level, I could not connect the .sks and the .swift file I had made. I tried following this tutorial exactly:

http://www.techotopia/index.php/An_iOS_8_Swift_Sprite_GameKit_Level_Tutorial_Editor_ArcheryC/a>

http://www.techotopia/index.php/An_iOS_8_Swift_Sprite_Kit_Level_Editor_Game_Tutorial#Creating_the_Archery_Scene

当我连接这两个文件时没有出现错误,然后当我转换到场景时它正确转换,但我在视觉上添加到 .sks 的任何内容都没有出现在模拟器中.请帮忙,我花了几个小时研究解决方案.我能找到的关于关卡编辑器的极少数教程和论坛只讨论了从新的 SpriteKit 项目开始的 GameScene.swift 和 .sks.

When I connected the two files no errors came up, and then when I transitioned to the scene it transitioned properly, but nothing that I added visually to the .sks showed up in the simulator. Please help, I've spent hours researching solutions for this. The very few tutorials and forums that I was able to find about the level editor only talked about the GameScene.swift and .sks that start with a new SpriteKit project.

推荐答案

此方法将解压 sks 文件并将其初始化为调用它的任何 SKNode 子类.这意味着您可以将文件解压缩为 SKNode(因此根节点将是 SKNode)并将其作为子节点添加到您的场景中.您还可以将文件解压缩为 GameScene 或任何 SKNode 子类.

This method will unarchive an sks file and initialise it as whichever SKNode subclass it is called on. This means you can unarchive a file as an SKNode (so the root node will be an SKNode) and add it as a child to your scene. You can also unarchive a file as GameScene or any SKNode subclass.

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

像这样使用它:

let scene = GameScene.unarchiveFromFile("GameScene")! as GameScene

let levelStuff = SKNode.unarchiveFromFile("Level 1")!
self.addChild(levelStuff)

这篇关于如何将 .sks 文件链接到 SpriteKit 中的 .swift 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-24 10:48:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1061442.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   如何将   链接   swift   sks

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!