gpt4 book ai didi

swift - 如何从 SKScene 获取和更改我的 View Controller 中的变量

转载 作者:搜寻专家 更新时间:2023-11-01 05:39:35 24 4
gpt4 key购买 nike

好的,这是代码

class GameViewController:  UIViewController,  SceneTransitionDelegate, 
GKGameCenterControllerDelegate, ADBannerViewDelegate {

var coolbool:Bool = false

...abunch of unimportant stuff functions and stuff

}

这是我在 SKScene 中尝试做的事情

func thing1()
{
let controller = GameViewController()
controller.coolbool = true
println(controller.coolbool) // Will say that it is true
sceneDelegate.transitionToScene(Menu.self) //Menu.self is the skscene that
we used to be in and will be in
}

func thing2()
{
println(controller.coolbool) // Will say that it is false
if (controller.coolbool == true)
{
//Put rainbows over every sprite and change generator settings
}
}

所以基本上发生的事情是“coolbool”被初始化为 false。直到调用 thing1() 导致变量“coolbool”发生变化。我在过渡之前立即确认它的变化。但是在转换之后(到同一个场景(如果 bool 为真,我试图让它看起来不同))如果你问这个值是什么,它说它是假的......即使我只是将它设置为真.

无论如何,我假设我做错了什么,他们是更好的方法吗???如果你想要它,这里是过渡函数

func transitionToScene(sceneClass:Scene.Type) {
playing = false
var sizeRect = UIScreen.mainScreen().applicationFrame
var width = sizeRect.size.width * UIScreen.mainScreen().scale
var height = sizeRect.size.height * UIScreen.mainScreen().scale
let skView = self.view as! SKView
let scene = sceneClass(size: skView.bounds.size)
scene.size = CGSizeMake(width, height)
rwidth = width
rheight = height
swidth = width
sheight = height
skView.ignoresSiblingOrder = true

scene.scaleMode = .AspectFill
scene.sceneDelegate = self

skView.presentScene(scene)
}

最佳答案

你需要获取GameViewController的当前实例,所以这条语句:

let controller = GameViewController()

不会工作,因为它声明了一个 Controller 的新实例,该实例与您当前使用的 SKView 无关。相反,我们需要获取当前呈现 View Controller 。

获取 View Controller 的实例:

这里可以帮助我们的最接近的方法是在 UIResponder 类中,它是 nextResponder:。它的一般功能是找到并返回当前的下一个响应者,这似乎很模糊,对我们来说用处不大,直到 Apple 提到:

Subclasses must override this method to set the next responder. UIView implements this method by returning the UIViewController object that manages it (if it has one) or its superview (if it doesn’t)

这意味着如果我们在场景中,我们可以使用场景的 self.view 找到 View Controller ,它是呈现 SKView 对象(继承自 UIView)。我们所要做的就是在我们的响应链中找到 View Controller 。

swift :

var currentViewController: GameViewController?
var upstreamResponder: UIResponder? = self.view
var found = false

while (found != true){
upstreamResponder = upstreamResponder!.nextResponder()
if let viewController = upstreamResponder as? GameViewController {
currentViewController = viewController
found = true
}
if upstreamResponder == nil {
//could not find VC, PANIC!
break
}
}

Objective-C:(可能必须#include "GameViewController.h")

GameViewController *currentViewController;
UIResponder *upstreamResponder = self.view;
BOOL found = false;

while (found != true) {
upstreamResponder = [upstreamResponder nextResponder];
if ([upstreamResponder isKindOfClass:[GameViewController class]]) {
currentViewController = (GameViewController *)upstreamResponder;
}
if (upstreamResponder == nil) {
//omg where did VC go?!
break;
}
}

在您的 View Controller 上设置属性

现在您有了当前的 View Controller ,设置任何所需的属性都非常简单。所以如果你想改变coolbool,你所要做的就是声明:

currentViewController.coolbool = true

希望对您有所帮助!
-最大

关于swift - 如何从 SKScene 获取和更改我的 View Controller 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32283319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com