【问题标题】:Moving from GameVC to another View Controller从 GameVC 移动到另一个视图控制器
【发布时间】:2017-09-15 08:09:07
【问题描述】:

我有一个应用程序,当您打开它时,它会带您进入主菜单,您可以在其中选择游戏的难度。按下主菜单中的任意按钮,即可加载游戏场景。

我的问题基本上是现在我想到达另一个视图控制器,它与主菜单具有相同的类,在达到一个条件后,例如得到10分或类似的分数。 我怎样才能做到这一点?我已经使用拖放操作来处理故事板了。

我是 swift 编程的新手,如果您能给我一些关于如何使它变得更好的建议,那就太好了。

编辑:我得到了这个代码:

func addScore(playerWhoWin: SKSpriteNode){

    ball.position = CGPoint(x: 0, y: 0)
    ball.physicsBody?.velocity = CGVector(dx: 0, dy: 0)

    if playerWhoWin == main {
        score[0] += 1
        ball.physicsBody?.applyImpulse(CGVector(dx: 22 , dy: 22))
    }
    else if playerWhoWin == enemy {
        score[1] += 1
        ball.physicsBody?.applyImpulse(CGVector(dx: -22 , dy: -22))
    }

    topLbl.text = "\(score[1])"
    btmLbl.text = "\(score[0])"
}

现在在我得到的更新功能中:

override func update(_ currentTime: TimeInterval) {

    switch currentGameType {
    case .easy:
        enemy.run(SKAction.moveTo(x: ball.position.x, duration: 1.4))
        break
    case .medium:
        enemy.run(SKAction.moveTo(x: ball.position.x, duration: 1.0))
        break
    case .hard:
        enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.7))
        break
    case .player2:
        break
    }

    if ball.position.y <= main.position.y - 70 {
        addScore(playerWhoWin: enemy)
            moveToWinScreen() // <- This is the function I'm trying to write
    }
    else if ball.position.y >= enemy.position.y + 70 {
        addScore(playerWhoWin: main)
            moveToWinScreen()
    }
}

游戏本身运行正常,我可以进入它,选择一个难度,然后在游戏场景中使用“菜单”按钮(我通过故事板中的拖放与主菜单链接)

现在我需要一个名为 moveToWinScreen 的函数,它应该如下所示:

func moveToWinScreen(){

    if score[0] > 9 {
        //here I want to move to the UIViewController that shows you that you won. This UIViewController got the storyboard ID: mainwinVC , and has the same class as the mainmenue: MenueVC.
    }
    if score[1] > 9 {
    //here I want to move to the UIViewController that shows you that your opponent won. This UIViewController got the storyboard ID: enemywinVC , and has the same class as the mainmenue: MenueVC.
    }
}

【问题讨论】:

  • 这太宽泛了。你有什么用?
  • 对此我很抱歉。很难解释自己,因为这对我来说是第一个 swift 项目。我得到了一个 MenueVC.swift ,其中链接了难度的按钮,以及难度的枚举在哪里。在 GameScene.swift 中,我声明了游戏的运作方式,它是一个乒乓球游戏,所以没什么难的。好吧,然后在 GameScene.sks 和 Main.storyboard 中进行一些建模,我会在一分钟内做一些截图
  • 我需要的基本上是这样的(现在是伪代码):if score[0] &gt; 10 {goto winningscreen0} if score[1] &gt; 10 {goto winningscreen1} 我得到了 2 个不同的 VC,每个 VC 都给获胜的玩家。

标签: ios swift3


【解决方案1】:

您必须为此创建一个自定义委托方法。

例如:raywenderlich sprite kit tutorial

在这个怪物游戏中,他们使用委托方法来计算怪物碰撞次数,方法是:

func projectileDidCollideWithMonster(projectile: SKSpriteNode, monster: SKSpriteNode) {
    print("Hit")
    projectile.removeFromParent()
    monster.removeFromParent()

    monstersDestroyed += 1
    if (monstersDestroyed > 30) {
        let reveal = SKTransition.flipHorizontal(withDuration: 0.5)
        let gameOverScene = GameOverScene(size: self.size, won: true)
        self.view?.presentScene(gameOverScene, transition: reveal)
    }
    print("score:",monstersDestroyed)
}

在上面的代码中,您可以看到如果实现了目标,它会将场景带到新视图。 游戏结束场景是另一个显示分数和结果的视图。

希望以上内容对您有所帮助。更多信息您可以通过上面的链接浏览。

【讨论】:

  • 但是如果我想去的屏幕是 UIViewcontroller 呢?
  • 然后只需推动视图控制器:让 control = storyboard?.instantiateViewController(withIdentifier: "Identifier") as!类名 self.navigationController?.pushViewController(control, animated: true) 代替游戏场景
  • @IPiiro 希望对您有所帮助
  • 让 storyboard = UIStoryboard(name: "Main", bundle: nil)
  • 您是否将视图控制器嵌入到导航控制器中??
【解决方案2】:

对于视图控制器

1.在你的游戏场景类中

class GameScene: SKScene {
var viewController: UIViewController?
}
  1. 然后在 GameViewController 类中

    就在skView.presentScene(scene)之前添加这行代码

    scene.viewController = self

现在,您可以直接访问 viewController。只需在这个 viewController 上调用 segue:

func returnToMainMenu(){
self.viewController.performSegueWithIdentifier("menu", sender: vc)
}

我已经实现并尝试了这段代码,它对我来说很好用

Refer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 2019-05-26
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    相关资源
    最近更新 更多