SpriteKit" START"在其中调用场景中的start()的按钮

时间:2016-12-11 23:47:46

标签: swift sprite-kit

如何在StartButton类中编写touchesBegun命令,该类在场景中调用start()并将其自身的任何实例放入?

我知道......可能是OOP 101.但今天超出我的范围。

UPDATE:

以下是我目前(部分)解决问题的方法。它感觉不对,但它有点起作用。不像我想的那样多:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    SoundManager.playSceneStartSound()
    run(ready)
     if CEO.startButtonIsActive{
        print("We're STARTING NOW...")
        if let menuScene = self.scene as? Menu_Memory {
            menuScene.startTheGame()
            }
        else
        if let menuScene = self.scene as? Menu_Chaser {
            menuScene.startTheGame()
            }
        else
        if let menuScene = self.scene as? Menu_Picker {
            menuScene.startTheGame()
            }
        else
        if let menuScene = self.scene as? Menu_Q_AndA {
            menuScene.startTheGame()
            }
        else
        {print("Houston, we have a problem... no gameScene")}
        }
    else
    {
    print("You still have some preparation to do, amigo!")
    }
}

或者,传递类类型的能力也会有所帮助。这几乎是一个单独的问题,但事实上我认为他们可能非常密切相关。

无论如何:在一个按钮中,尝试创建对类类型的引用,我尝试了很多变种,没有运气。

我的一些荒谬的尝试都失败了:

Menu_Memory是SKScene

    var menuClass01: Menu_Memory.self
    var menuClass02: AnyClass = Menu_Memory.self
    var menuClass03: AnyClass = Menu_Memory(self)
    var menuClass04: AnyClass as! Menu_Memory.self
    var menuClass05: AnyClass as! Menu_Memory(self)

1 个答案:

答案 0 :(得分:1)

更新 现在我知道你在做什么。应该是自我解释。如果不是,请对我大喊:

public extension SKScene {
  func startGame() { // Because startTheGame is a terriblename..
    // Override in your subclasses of SKScene if desired.
  }
}

enum Menu { // Used as namespace for chaser and menu classes

  final class Chaser: SKScene {
    override func startGame() {
      print("chaser started")
    }
  }

  final class Memory: SKScene {
      override func startGame() {
      print("memory started")
    }
  }
}

final class Button: SKNode {
  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    scene!.startGame() // No need for guard / if-let,
                       // because how can you click the node if its scene is nil :)?
  }
}