我在这个线程How to get from SKScene to UIViewController?中也遇到类似的问题。首先,我应该说我是一个完全的初学者,这对我来说是全新的。

我的主菜单(加载应用后的第一个屏幕)在此处:

class MenuVC: UIViewController {

... }


我的应用(游戏)的交互可以在这里进行:

    //
//  GameScene.swift
//  Doodle Pong
//
//  Created by Daniel Kloe on 28.02.17.
//  Copyright © 2017 Daniel Kloe. All rights reserved.
//

import SpriteKit
import GameplayKit
import Foundation
import UIKit


class GameScene: SKScene {

//let storyboard = UIStoryboard(name: "Main", bundle: nil)

//let MenuVC = storyboard.instantiateViewController(withIdentifier: "MenuVC")

//MenuVC.view.frame = (self.view?.frame)!

//MenuVC.view.layoutIfNeeded()


//UIView.transition(with: self.view!, duration: 0.3, options: .transitionFlipFromRight, animations:

//{

//self.view?.window?.rootViewController = vc

//}, completion: { completed in

//})



var ball = SKSpriteNode()
var enemy = SKSpriteNode()
var main = SKSpriteNode()
var score = [Int] ()

var topLbl = SKLabelNode()
var btmLbl = SKLabelNode()

override func didMove(to view: SKView) {

    topLbl = self.childNode(withName: "topLabel") as! SKLabelNode
    btmLbl = self.childNode(withName: "btmLabel") as! SKLabelNode

    ball = self.childNode(withName: "ball") as! SKSpriteNode
    enemy = self.childNode(withName: "enemy") as! SKSpriteNode
    main = self.childNode(withName: "main") as! SKSpriteNode


    let border = SKPhysicsBody(edgeLoopFrom: self.frame)
    border.friction = 0
    border.restitution = 1
    self.physicsBody = border

    startGame()

}

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

    for touch in touches{


       // let gameSceneTemp = GameScene(fileNamed: "GameScene")
       // self.scene?.view?.presentScene(gameSceneTemp, transition: SKTransition.doorsCloseHorizontal(withDuration: 0.01))

        let location = touch.location(in: self) //die location wird mit dem Berühren auf dem Bildschirm beschrieben

        if currentGameType == .TwoPlayer{

            if location.y > 0{

                enemy.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
            }

            if location.y < 0{

                main.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt

            }

        }

        else{

            main.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt

        }


    }
}

func startGame(){

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

    //let delay = SKAction.wait(forDuration: 2000)
    //self.run(delay) //evtl. Wartezeit 2s, funktioniert noch nicht richtig

    ball.physicsBody?.applyImpulse(CGVector(dx: 20, dy: 20))

}

func addScore(playerWhoWon : SKSpriteNode){


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


    if playerWhoWon == enemy{

        score[1] += 1
        ball.physicsBody?.applyImpulse(CGVector(dx: 32, dy: 27))


    }

    else if playerWhoWon == main{


        score[0] += 1
        ball.physicsBody?.applyImpulse(CGVector(dx: -32, dy: -27))

    }

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


}

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

    for touch in touches{

        let location = touch.location(in: self) //die location wird mit dem Berühren auf dem Bildschirm beschrieben

        if currentGameType == .TwoPlayer{

            if location.y > 0{

                enemy.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
            }

            if location.y < 0{

                 main.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt

            }

        }

        else{

            self.topLbl.zRotation = CGFloat(2 * M_PI)
           // UIView.animate(withDuration: 0, animations: ({

               // self.topLbl.zRotation = CGFloat(2 * M_PI) //CGFloat(.pi / 4.0)

           // }))

            main.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt

        }

    }


}

override func update(_ currentTime: TimeInterval) {

    switch currentGameType{

    case .Easy:
        enemy.run(SKAction.moveTo(x: ball.position.x, duration: 1.0))
        break

    case .Medium:
        enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.5))
        break

    case .Extreme:
        enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.08))
        break

    case .TwoPlayer:

        break

    }

     //Called before each frame is rendered
    if ball.position.y <= main.position.y - 20{
        addScore(playerWhoWon: enemy)

    }

     else if ball.position.y >= enemy.position.y + 20{
       addScore(playerWhoWon: main)


    }

}



@IBAction func backToMainMenu(_ sender: Any) {
     //self.view?.window?.rootViewController
    self.view?.window!.rootViewController?.dismiss(animated: false, completion: nil)
}


}


在我的函数“ backToMainMenu”中,我尝试返回到MenuVC,但收到警告“ UIViewController类型的表达?”。尚未使用”,如果我在模拟器中尝试,模拟器将崩溃。

我非常感谢您的各种帮助:)。

最佳答案

尝试:

self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)

这应该关闭rootViewController以上的所有视图

编辑:

当我们定义ViewController时,这可能对您有用,请确保您的Storyboard中的MenuVC具有标识符:

let storyboard = UIStoryboard(name: "Main", bundle: nil)

        let vc = storyboard.instantiateViewController(withIdentifier: "MenuVC") // Make sure is the same identifier as in the storyboard.

        vc.view.frame = (self.view?.frame)!

        vc.view.layoutIfNeeded()


        UIView.transition(with: self.view!, duration: 0.3, options: .transitionFlipFromRight, animations:

            {

                self.view?.window?.rootViewController = vc

        }, completion: { completed in

        })

    }


编辑:

看来最好的方法是在另一个视图控制器中托管您的SKScene类。我建议在您的SKScene所在的位置创建另一个ViewController,例如SKViewController。将您的MenuVC嵌入navigationController内,在storyBoard中单击它,然后单击“编辑器”(从顶部菜单)>嵌入>导航控制器。编辑MenuVC,以便仅打开SKViewController并在此处打开场景,而不是打开场景。为此,只需从情节提要中的MenuVC创建一个序列,例如,使用标识符“ MenuToSKScene”,当您要打开场景时,可以从MenuVC中执行以下操作:

  self.performsegue(with identifier: "MenuToSKScene", sender: self)


然后在MenuVCSKViewController中执行当前操作,而不是打开场景。

这会将您发送到具有SKScene类的SKViewController。然后,当您想返回菜单时,您要做的就是:

 self.dismiss(animated: true, completion: nil)


或创建一个从SKViewController到MenuVC的标识为“ SKSceneToMenu”的序列,然后调用:

self.performsegue(with identifier: "SKSceneToMenu", sender: self)


因此,从本质上讲,不要从SKScene打开MenuVC,而是要打开一个单独的viewController SKViewController,以便在打开场景时将其打开,以便可以轻松跳回菜单。

关于ios - 如何从SKScene返回到UIViewController(MenuVC)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55107785/

10-11 11:12