Swift - SKSprite Kit - Change Gamescene Variables from FileBrowser Closure

自闭症网瘾萝莉.ら 提交于 2020-08-10 19:17:40

问题


I am working on an application that uses the 'FileBrowser' CocoaPod to load files in a simulator (using the SKSprites Physics Engine)

I have a function in my GameViewController that creates a 'FileBrowser' instance, then allows the user to set it's 'didSelectFile' closure like so:

let file = FileBrowser()
    present(file, animated: true, completion: nil)

fileBrowser.didSelectFile = { (file: FBFile) -> Void in
print(file.displayName)}

This enables me to trigger that small bit of code in the closure once a file has been selected from the popup browser, and to print the name of the file clicked, but I can't figure out how to get the value of 'file.displayName' back to my GameScene (I need it to be passed to that class instance to load the file and change the level, etc).

I've tried adding a 'String' return to the closure, which I cannot do. I've also tried to pass my GameScene to the Closure but that doesn't work.

So how do I get the data that is retrievable within that closure back to my GameScene if I cannot edit the closures inputs / outputs? (they are defined by the library).

Thanks


回答1:


I solved my problem:

What I had to do was give the GameViewController class a property 'scene', and then add inside of the "viewDidLoad()" function:

self.scene = gameScene

then inside the closure, I had to add the code

let gameScene = self.scene as! GameScene

Then I could edit variables in my GameScene (ex: gameScene.numberofpoints = 100)

So in short, I guess giving the GameViewController class a 'scene' variable to store the gamescene, then allowed me to change attributes of this gameScene from within the closure.



来源:https://stackoverflow.com/questions/63293018/swift-sksprite-kit-change-gamescene-variables-from-filebrowser-closure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!