【问题标题】:How to edit a property of GameViewController from GameScene如何从 GameScene 编辑 GameViewController 的属性
【发布时间】:2017-05-05 04:50:33
【问题描述】:

我希望在场景前出现一个视图来模拟文本框旁白。由于 SklabelNode 的限制,我更喜欢使用 UILabel 的动画功能和文本换行功能。

gameScene.swift 中的代码

var viewController = GameViewController()

override func didMove(to view: SKView) {

        viewController.printStuff()

        viewController.uiLabelContainer.isHidden = false

    }

GameViewController.swift 中的代码

override func viewDidLoad() {
        super.viewDidLoad()

        // Load 'GameScene.sks' as a GKScene. This provides gameplay related content
        // including entities and graphs.
        if let scene = GKScene(fileNamed: "GameScene") {

            // Get the SKScene from the loaded GKScene
            if let sceneNode = scene.rootNode as! GameScene? {

                // Copy gameplay related content over to the scene
                sceneNode.entities = scene.entities
                sceneNode.graphs = scene.graphs

                // Set the scale mode to scale to fit the window
                sceneNode.scaleMode = .aspectFill
                sceneNode.viewController = self
                // Present the scene
                if let view = self.view as! SKView? {
                    view.presentScene(sceneNode)
                    view.ignoresSiblingOrder = true

                    view.showsFPS = true
                    view.showsNodeCount = true
                }
            }
        }
        uiLabelContainer.isHidden = true

    }

func printStuff()  {
        print("this works, calling from gameviewcontroller")
    }

从 gameviewcontroller 调用函数可以工作,甚至可以打印 UILabel 上的内容(访问来自另一个类的变量或对象的数据)。但是,将其中一个对象(uiLabelContainer)的属性从 gameViewController 更改为 .isHidden = false 似乎不起作用。

【问题讨论】:

    标签: swift3 sprite-kit


    【解决方案1】:

    发生这种情况是因为您在场景中重新创建了 GameViewController 行:

    var viewController = GameViewController()
    

    而不是获取已经存在的GameViewController

    有很多方法可以解决您的问题。 因此,例如使用“hello worldSprite-kit 模板,如果您将 GameViewController 作为游戏的初始视图控制器,您可以这样做:

    游戏视图控制器:

    import UIKit
    import SpriteKit
    class GameViewController: UIViewController {
        var uiLabelContainer:UILabel!
        override func viewDidLoad() {
            super.viewDidLoad()
            uiLabelContainer = UILabel(frame: CGRect(x:0,y: 0,width: 250,height: 50))
            uiLabelContainer.textAlignment = NSTextAlignment.left
            uiLabelContainer.textColor = .white
            uiLabelContainer.text = "This is a Label"
            self.view.addSubview(uiLabelContainer)
            uiLabelContainer.isHidden = true
            if let view = self.view as! SKView? {
                if let scene = SKScene(fileNamed: "GameScene") {
                    scene.scaleMode = .aspectFill
                    view.presentScene(scene)
                }
                view.ignoresSiblingOrder = true
                view.showsFPS = true
                view.showsNodeCount = true
            }
        }
        func printStuff() {
            print("printStuff")
        }
    }
    

    游戏场景:

    import SpriteKit
    class GameScene: SKScene {
        private var label : SKLabelNode?
        var viewController : GameViewController!
        override func didMove(to view: SKView) {
            self.label = self.childNode(withName: "//helloLabel") as? SKLabelNode
            if let label = self.label {
                label.alpha = 0.0
                label.run(SKAction.fadeIn(withDuration: 2.0))
            }
            self.run(SKAction.wait(forDuration: 2),completion:{[unowned self] in
                let appDelegate = UIApplication.shared.delegate as! AppDelegate
                if let vc = appDelegate.window?.rootViewController {
                    self.viewController = vc as! GameViewController
                    self.viewController.uiLabelContainer.isHidden = false
                }
            })
            self.run(SKAction.wait(forDuration: 5),completion:{[unowned self] in
                let scene2 = GameScene2()
                scene2.scaleMode = .aspectFill
                self.view?.presentScene(scene2)
            })
        }
        deinit {
            print("\n THE SCENE \((type(of: self))) WAS REMOVED FROM MEMORY (DEINIT) \n")
        }
    }
    

    游戏场景2:

    (仅为测试而创建,以查看正确的GameScene 释放)

    import SpriteKit
    class GameScene2: SKScene {
        private var label : SKLabelNode?
        override func didMove(to view: SKView) {
            print("gameScene2")
        }
    }
    

    输出

    【讨论】:

    • 谢谢你!这里的这行代码: let appDelegate = UIApplication.shared.delegate as! AppDelegate if let vc = appDelegate.window?.rootViewController { self.viewController = vc as! GameViewController self.viewController.uiLabelContainer.isHidden = false } 是关键!!!
    • 你是救生员! :)
    • 我为此搜索了互联网。这绝对是这里的关键。让 appDelegate = UIApplication.shared.delegate 为!应用委托;如果让 vc = appDelegate.window?.rootViewController { self.viewController = vc as!游戏视图控制器 }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多