这是我的相关代码:

来自我的GameScene类:

    func resetCircleAndScore(scale: CGFloat)
{
    circleIsStationary = true
    currScore = 0
    CIRCLE.speed = 1
    CIRCLE.physicsBody!.velocity = CGVector(dx: 0,dy: 0)
    CIRCLE.position = CGPointMake(midX, midY)
    circleScale = scale
    circleSpeed = CONSTSPEED
    CIRCLE.setScale(circleScale)
    currScoreLabel.text = "SCORE: " + Int(currScore).description
    //need to update modeSelected here
}

从我的GameViewController类:
public func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
    modeCopy = modes[row]

    println(modeCopy)
}

我试图为modeCopy添加一个吸气剂,如下所示:
public func getMode() -> String
{
    return modeCopy
}

问题是,当我尝试从另一个类调用此函数时,它需要一个GameViewController类型的名为“self”的参数。我真正想做的是访问创建我的GameScene的GameViewController实例。我也可以访问GameViewController创建的GameScene实例,但是我不确定该怎么做。我在GameViewController中的ViewDidLoad如下。
override public func viewDidLoad()
{
    self.pickerView.dataSource = self
    self.pickerView.delegate = self

    println("got to beginning of viewDidLoad")
    super.viewDidLoad()

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene
    {
        // Configure the view.
        let skView = self.view as SKView
        let showStats: Bool = true
        skView.showsFPS = showStats
        skView.showsNodeCount = showStats

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)
    }
}

最佳答案

如果GameScene是您自己的类,则可以添加一个GameViewController类型的可选参数。

class GameScene {
    weak var parentController: GameViewController?

....
}

在取消存档GameView时,在viewDidLoad()方法中执行此操作
scene.parentController = self

现在您可以在GameScene中使用它

关于ios - 如何在Swift中访问GameViewController特定实例的变量的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29306542/

10-12 14:33