在iOS中点击标签后是否可以更改场景?

时间:2017-06-12 14:23:46

标签: ios swift sprite-kit scene

我有一个学校项目来制作一个很好的游戏,但我很难添加一个后退按钮。我有一个想法点击后面的单词带你到菜单,但我不知道该怎么做。正常的“按钮”不可用,因为它从精灵套件场景返回。任何建议或帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

您可以向UIButton添加正常SKView,但这可能不是最干净的解决方案。您可以使用SKLabelNode touchesBegantouchesEnded上的触摸功能代替我们。我以这种方式为SpriteKit做了一个简单的按钮实现。你可以在GitHub here上查看。

答案 1 :(得分:1)

你走了:

MenuVC.swift:

enum gameType {
  case easy
  case wobble
  case player2
}

var currentGameType: gameType = .easy
var navController = UINavigationController()
var storyBoard = UIStoryboard()

func presentMenuVC() {
  let menuVC = storyBoard.instantiateViewController(withIdentifier: "menuVC") as! MenuVC
  navController.pushViewController(menuVC, animated: true)
}

class MenuVC : UIViewController {

    @IBAction func Player2(_ sender: Any) {
        moveToGame(game: .player2)
    }
    @IBAction func Easy(_ sender: Any) {
        moveToGame(game: .easy)
    }
    @IBAction func Wobble(_ sender: Any) {
        moveToGame(game: .wobble)
    }

    func moveToGame(game: gameType) {

      navController = self.navigationController!
      storyBoard = self.storyboard!

      let gameVC = self.storyboard?.instantiateViewController(withIdentifier: "gameVC") as! GameViewController

        currentGameType = game

        self.navigationController?.pushViewController(gameVC, animated: true)

    }
}

NewFile.swift:

class MainMenuButton: SKSpriteNode {

  init(text: String, font: String) {
    let label = SKLabelNode(text: text)
    label.fontName = font

    let texture = SKView().texture(from: label)

    super.init(texture: texture!, color: .clear, size: texture!.size())
    isUserInteractionEnabled = true
  }

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    presentMenuVC()
  }

  required init?(coder aDecoder: NSCoder) { fatalError() }
}

GameScene.swift(添加到底​​部的d​​idMoveToView函数):

addChild(MainMenuButton(text: "go back", font: "Chalkduster"))