2

Ok so here is the code

class GameViewController:  UIViewController,  SceneTransitionDelegate, 
GKGameCenterControllerDelegate, ADBannerViewDelegate {

var coolbool:Bool = false

...abunch of unimportant stuff functions and stuff

 }

And here is what I am trying to do from my 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
        }
}

So basically what happens is that "coolbool" is initialized as being false. Until thing1() is called causing the variable "coolbool " to change. And i confirm its change immediately after, before the transition. However after the transition (to the same scene (I'm trying to make it look different if the bool is true)) if you ask what the value is, it says its false.... even though i just set it to true.

Anyway I assume I am doing something wrong, is their a better way to do this??? Incase you want it here is the transition function

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

1 Answers1

0

You need to get the current instance of the GameViewController, so this statement:

let controller = GameViewController()

isn't going to work because it declares a new instance of a controller, which is not related to the SKView you are currently using. Instead, we need to obtain the current, presenting view controller.

Obtaining Instance of the View Controller:

The closest method that can help us here is in the UIResponder class, and it is nextResponder:. Its general function is to find and return the current next responder, which seems vague and not of much use to us, until Apple mentions that:

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)

This means that if we are in a scene, we can find the view controller by using the scene's self.view which is the presenting SKView object (inherits from UIView). All we have to do is find the View Controller in our responder chain.

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: (will probably have to #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;
    }
}

Setting properties on your View Controller

Now that you have your current view controller, it is very simple to set any desired property. So if you wanted to change coolbool, all you have to do is state:

currentViewController.coolbool = true

Hope this helps!
-Max

MaxKargin
  • 1,560
  • 11
  • 13