jjzjj

ios - 在 swift spritekit 中正确呈现新场景

coder 2023-09-08 原文

我正在使用 spritekit 和 swift,我只是想呈现一个新场景……但是出了点问题并抛出错误。我很确定这是正确的语法,这真的让我陷入困境。

线

    let skView = self.view as SKView

给我的错误是“SKView? is not convertible to SKView

感谢任何建议! *我当前的代码如下:

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches{
        let location = touch.locationInNode(self)
        if(self.nodeAtPoint(location) == self.playButton)
            {
                var scene = PlayScene(size: self.size)
                let skView = self.view as SKView
                    // Configure the view.

                /* 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

                skView.presentScene(scene)
            }
        }
    }

最佳答案

问题是 SKScene 上的 view 属性是可选的 (SKView?),因为 SKScene 没有' 必须有一个包容的观点;在它被呈现之前它包含什么?没有什么。

要解决您的问题,您需要通过展开 view 属性来检查场景是否具有 View ,例如使用可选绑定(bind):

if let view = self.view {
    let scene = PlayScene(size: self.size)
    scene.scaleMode = .AspectFill
    view.presentScene(scene)
}

如果有人按下它,您可以非常确定您的 SKScene 已经呈现(因此 view 不是 nil)-因此你可以强制解包view,像这样:

let scene = PlayScene(size: self.size)
scene.scaleMode = .AspectFill
view!.presentScene(scene)

此外,您不需要再次配置 SKView (skView.ignoresSiblingOrder = true),因为据推测,这已经在 GameViewController


编辑:

let skView = self.view as! SKView 可能来自 GameViewController(UIViewController 的子类)。如果您查看 UIViewController 的文档和 SKScene你会看到它们都有一个 view 属性:

class UIViewController: /* Superclass and Protocols */ {
    var view: UIView!
    // ...
}

class SKScene: SKEffectNode {
    weak var view: SKView? 
    // ...
}

但是它们的类型不同,这意味着您使用它们的方式不同。在 SKScene 的情况下,请参阅上面关于解包的内容。对于 UIViewController,您需要使用以下行转换为 SKView:

let skView = self.view as! SKView

因为 UIView 没有呈现 SKScene 所需的方法。 (有关转换的更多信息,我建议您查看 The Swift Programming Language: Type Casting)


通常您不会被允许从 UIView 转换为 SKView(请记住 SKView 的子类UIView).在 Playgrounds 中尝试以下操作:

let view = UIView()
let skView = view as! SKView

您应该会收到类似以下内容的错误:

Could not cast value of type 'UIView' (0x1041d0eb0) to 'SKView' (0x10da10718)

但是,您可以在 GameViewController 中从 UIView 转换为 SKView,因为 Custom Class GameViewControllerview 已设置为 SKView:

我希望这有助于消除您的任何困惑。

关于ios - 在 swift spritekit 中正确呈现新场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30583099/

有关ios - 在 swift spritekit 中正确呈现新场景的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  4. ruby - Hanami link_to 助手只呈现最后一个元素 - 2

    我是HanamiWorld的新人。我已经写了这段代码:moduleWeb::Views::HomeclassIndexincludeWeb::ViewincludeHanami::Helpers::HtmlHelperdeftitlehtml.headerdoh1'Testsearchengine',id:'title'hrdiv(id:'test')dolink_to('Home',"/",class:'mnu_orizontal')link_to('About',"/",class:'mnu_orizontal')endendendendend我在模板上调用了title方法。htm

  5. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  6. ruby - 为 IO::popen 拯救 "command not found" - 2

    当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby​​1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#

  7. ruby-on-rails - 我将 Rails3 与 tinymce 一起使用。如何呈现用户关闭浏览器javascript然后输入xss? - 2

    我有一个用Rails3编写的站点。我的帖子模型有一个名为“内容”的文本列。在帖子面板中,html表单使用tinymce将“content”列设置为textarea字段。在首页,因为使用了tinymce,post.html.erb的代码需要用这样的原始方法来实现。.好的,现在如果我关闭浏览器javascript,这个文本区域可以在没有tinymce的情况下输入,也许用户会输入任何xss,比如alert('xss');.我的前台会显示那个警告框。我尝试sanitize(@post.content)在posts_controller中,但sanitize方法将相互过滤tinymce样式。例如

  8. ruby - IO::EAGAINWaitReadable:资源暂时不可用 - 读取会阻塞 - 2

    当我尝试使用“套接字”库中的方法“read_nonblock”时出现以下错误IO::EAGAINWaitReadable:Resourcetemporarilyunavailable-readwouldblock但是当我通过终端上的IRB尝试时它工作正常如何让它读取缓冲区? 最佳答案 IgetthefollowingerrorwhenItrytousethemethod"read_nonblock"fromthe"socket"library当缓冲区中的数据未准备好时,这是预期的行为。由于异常IO::EAGAINWaitReadab

  9. ruby-on-rails - Rails 不呈现 public/index.html 文件;浏览器中的空白页面 - 2

    当我将我的Rails+React应用程序部署到Heroku时,我遇到了问题。React客户端位于Rails应用程序的client/目录中。由于使用了react-router,Rails服务器需要知道从React构建中渲染index.html。当我在Heroku上部署客户端时,脚本将内容从client/build/.复制到Rails应用程序的public/目录。现在问题来了:当我的路由检测到类似example.com/about的路径时,它会尝试呈现public/index.html。方法如下:deffallback_index_htmlrenderfile:"public/index.

  10. ruby-on-rails - 如何在 Rails 中正确呈现嵌套评论? - 2

    我试图让嵌套评论在Rails5应用程序中正常工作,但遇到了很多困难。我正在构建一个问答网站,我有一个Acomment模型,其中有属于答案的评论,还有属于其他评论的评论:classAcomment我想显示所有评论,然后显示所有对评论的回复,以及对回复的回复等。但是我不知道如何使嵌套正常工作。在我看来,在每个循环中,我正在渲染_comment.html.erb部分:"comment",:object=>comment%>然后,在我的_comment.html.erb局部中,我显示评论、回复链接,然后呈现评论回复的局部:comment,:answer_id=>comment.answer)i

随机推荐