Swift - SKSprite Kit - 从 FileBrowser 关闭更改游戏场景变量 [英] Swift - SKSprite Kit - Change Gamescene Variables from FileBrowser Closure

查看:12
本文介绍了Swift - SKSprite Kit - 从 FileBrowser 关闭更改游戏场景变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序使用FileBrowser"CocoaPod 在模拟器中加载文件(使用 SKSprites 物理引擎)

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

我的 GameViewController 中有一个函数可以创建一个FileBrowser"实例,然后允许用户像这样设置它的didSelectFile"闭包:

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)}

这使我能够在从弹出式浏览器中选择文件后触发闭包中的一小段代码,并打印单击的文件的名称,但我不知道如何获取'file.displayName' 返回到我的 GameScene(我需要将它传递给该类实例以加载文件并更改级别等).

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).

我尝试在闭包中添加一个字符串"返回值,但我做不到.我也尝试将我的 GameScene 传递给 Closure,但这不起作用.

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.

那么,如果我无法编辑闭包的输入/输出,我该如何将在该闭包内可检索的数据返回到我的 GameScene?(它们由库定义).

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).

谢谢

推荐答案

我解决了我的问题:

我必须做的是给 GameViewController 类一个属性场景",然后在viewDidLoad()"内部添加一个属性.功能:

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

然后我可以在我的 GameScene 中编辑变量(例如:gameScene.numberofpoints = 100)

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

简而言之,我想给 GameViewController 类一个场景"变量来存储游戏场景,然后允许我从闭包中更改这个 gameScene 的属性.

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.

这篇关于Swift - SKSprite Kit - 从 FileBrowser 关闭更改游戏场景变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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