本文介绍了如何在SpriteKit中使用委托协议模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 UIViewController 和两个 SKScene s。 UIViewController 通过旋转设备来显示两个场景。 风景加载 SKScene 1和肖像加载 SKScene 2就是这样:

So I have one UIViewController and two SKScenes. The UIViewController present the two scenes by rotating the device; landscape load SKScene 1 and portrait load SKScene 2 lie this:

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        print("TRIGGERED")

        if UIDevice.current.orientation.isLandscape {
            print("Landscape")
            presentView(name: "GameScene")
        } else {
            print("Portrait")
            presentView(name: "GameScene2")
        }
        super.viewWillTransition(to: size, with: coordinator)

    }

    func presentView(name: String) {
        if let view = self.view as! SKView? {
              if let scene = SKScene(fileNamed: name) {
                  scene.scaleMode = .aspectFill
                  view.presentScene(scene)
              }
              view.ignoresSiblingOrder = true
              view.showsFPS = true
              view.showsNodeCount = true
          }
    }

这很好。但是,我的难题是,我想在两个SKScenes GameScene GameScene2 之间实现委托协议模式。问题是 SKScene 1的 UIViewController (自身)应该作为<$ c $的委托传递c> SKScene 2.因此,例如,如果用户单击 landscape 模式(GameScene)模式中的某项,则som数据 String 会在方向更改为 portrait 后传递给GameScene2。

This works fine. My dilema, however, is that I want to implement a delegate-protocol pattern between the two SKScenes GameScene and GameScene2. The problem is that the UIViewController (self) of SKScene 1 should be passed as the delegate to the SKScene 2. So if the user for instance click something in landscape mode (GameScene), som data String is passed over to GameScene2 when orientation is changed to portrait.

所以在 SkScene 1中:

protocol MyProtocol {
    func myProtocolFunc(someString: String)
}

class GameScene: SKScene{

    var myDelegate: MyProtocol!

以及在 UIViewController 中如何设置代表? scene.myDelegate 不起作用。 GameScene()。myDelegate = GameScene2()。self 可以工作,但是由于 view.presentScene(scene)用于呈现不相关的场景。如何访问和设置委托?如何设置 delegate-protocol 模式,以便获得所需的结果?

and in the UIViewController how do I set the delegate? scene.myDelegate doesn't work. GameScene().myDelegate = GameScene2().self works but since view.presentScene(scene) is used to present the scene that's not relevant. How do I access and set the delegate? How do I set up the delegate-protocol pattern so that I get the desired result?

推荐答案

使用 Swift 5

protocol MyProtocol: AnyObject {
    func myProtocolFunc(someString: String)
}

class GameScene: SKScene {
    weak var myDelegate: MyProtocol?

    init(fileNamed: String) {
        super.init(fileNamed: fileNamed)
    }
}

然后,您必须在presentView方法中将场景对象创建为GameScene,而不是SKScene(就像您所做的那样)。因此,而不是:

Then you have to create the scene object in your presentView method as a GameScene, not a SKScene (as you did). So instead of:

if let scene = SKScene(fileNamed: name) // wrong

您必须:

if let scene = GameScene(fileNamed: name) // correct

然后可以使用 scene.myDelegate

这篇关于如何在SpriteKit中使用委托协议模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:31