1

我有一个 GameViewController,它在不同场景之间转换,例如:CircleScene 到 SquareScene,来回转换。

只要 GameViewController 是“初始视图控制器”,上述所有逻辑都可以正常工作。

一旦我在 GameViewController 上方有了另一个视图控制器,GameViewController 的屏幕转换就不再起作用了。

GameViewController.swift

import UIKit
import SpriteKit
import GameplayKit

protocol ScreenManager {
    func gotoSquare()
    func gotoCircle()
}

class GameViewController: UIViewController, ScreenManager {

    let sceneHeight = 380
    let sceneWidth = 570

    func gotoCircle() {
        let transition = SKTransition.push(with: .right, duration: 1.0)
        let newScene = CircleScene(size: CGSize(width: sceneWidth, height: sceneHeight))
        newScene.screenManager = self
        newScene.scaleMode = .aspectFill
        let view = self.view as! SKView?
        view?.presentScene(newScene, transition: transition)
    }

    func gotoSquare() {
        let transition = SKTransition.push(with: .left, duration: 1.0)
        let newScene = SquareScene(size: CGSize(width: sceneWidth, height: sceneHeight))
        newScene.screenManager = self
        newScene.scaleMode = .aspectFill
        let view = self.view as! SKView?
        view?.presentScene(newScene, transition: transition)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if let view = self.view as! SKView? {

            let scene = CircleScene(size: CGSize(width: sceneWidth, height: sceneHeight))
            scene.screenManager = self

            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill

            // Present the scene
            view.presentScene(scene)

            view.ignoresSiblingOrder = true
            view.showsFPS = true
            view.showsNodeCount = true
        }
    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .landscape
        } else {
            return .all
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }
}

MainViewContoller.swift

import UIKit

class MainViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func btnPressed(_ sender: AnyObject) {

        // Go to GameViewController
        let gameViewController:GameViewController = storyboard?.instantiateViewController(withIdentifier: "GameViewControllerID") as! GameViewController

        self.present(gameViewController, animated:false)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .landscape
        } else {
            return .all
        }
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

我尝试过 ViewController 方法(呈现/显示)失败。ViewController 演示是否有任何问题导致场景转换失败?感谢你的帮助。

4

0 回答 0