【发布时间】: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] > 10 {goto winningscreen0} if score[1] > 10 {goto winningscreen1}我得到了 2 个不同的 VC,每个 VC 都给获胜的玩家。