协议不会被执行Swift 3

时间:2017-02-04 11:39:37

标签: ios swift sprite-kit swift3 protocols

我用swift编写简单的数学游戏,我的协议有问题。但首先,我有3个UIViewController's,2个SKScene和一个NavigationViewControllerenter image description here

第一个菜单是游戏模式菜单,第二个是困难菜单,我想第二个是交互式并且有重力,所以我决定在SKScene中进行。 enter image description here

当用户点击菜单按钮时,它应该返回主菜单。它仅在我从主菜单进入但用户输掉游戏并从gameScene返回困难菜单时才有效,然后点击菜单按钮协议不会被执行。我不知道为什么,因为它应该。 (enum skad1是枚举,包含显示困难菜单的位置(从主菜单或游戏中))

我的主菜单代码:

    import UIKit

class MyMainViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func dividingclicked(_ sender: UIButton) {
    skad = .menu
    let gameVC = self.storyboard?.instantiateViewController(withIdentifier: "gamemenu")
    navigationController?.pushViewController(gameVC!, animated: true)    
}


}

我的困难菜单ViewController代码:

import UIKit
import SpriteKit
import GameplayKit

enum skad1 {
 case gra,menu
}
var skad = skad1.gra

class MyGVViewController: UIViewController, GameViewControllerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
        if let view = self.view as! SKView? {
        // Load the SKScene from 'GameScene.sks'
        if let scene = SKScene(fileNamed: "Menugame") {
            let gameScene = scene as! MySKScene
            gameScene.gameViewControllerDelegate = self
            gameScene.scaleMode = .aspectFill


            gameScene.size = view.bounds.size

            // Present the scene
            view.presentScene(gameScene)
        }

        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 .allButUpsideDown
    } 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
}

func goback() {
    print("goback executed")
    let view = self.view as! SKView?
    view?.presentScene(nil)
    navigationController?.popViewController(animated: true)

}
func goback2() {
    print("goback2 executed")
    let view = self.view as! SKView?
    view?.presentScene(nil)
    }

    }

我的问题菜单SkScene代码:

import GameKit
import SpriteKit
import CoreMotion

protocol GameViewControllerDelegate: class {
func goback()
func goback2()
}



class MySKScene: SKScene {

//Variables

var motionManager: CMMotionManager!
var ball = SKSpriteNode()
var ball1 = SKSpriteNode()
var ball2 = SKSpriteNode()
var ball3 = SKSpriteNode()
var ball4 = SKSpriteNode()
weak var gameViewControllerDelegate: GameViewControllerDelegate?

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

    for touch in touches {
        let location = touch.location(in: self)
        if ball.contains(location){
            showgame(zakres: 200)
        }else if ball1.contains(location){
            showgame(zakres: 100)
        }else if ball2.contains(location) {
            showgame(zakres: 50)
        }else if ball3.contains(location) {
            showgame(zakres: 150)
        }else if ball4.contains(location) {
            if skad == .menu{
                print("should execut goback")
                self.gameViewControllerDelegate?.goback()
            }else{
                print("should execut goback2")
                self.view?.presentScene(nil)
                gameViewControllerDelegate?.goback2()
            }
        }
    }
}

func showgame(zakres:Int) {

    if let view = self.view {
        range = zakres
        // Load the SKScene from 'GameScene.sks'
        if let scene = SKScene(fileNamed: "GameScene") {
            // Set the scale mode to scale to fit the window
            let gameScene = scene as! GameScene
            gameScene.scaleMode = .aspectFill

            // Present the scene
            gameScene.size = view.bounds.size
            view.presentScene(gameScene)
        }

        view.ignoresSiblingOrder = true

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



override func didMove(to view: SKView) {

    ball = self.childNode(withName: "ball") as! SKSpriteNode
    ball1 = self.childNode(withName: "ball1") as! SKSpriteNode
    ball2 = self.childNode(withName: "ball2") as! SKSpriteNode
    ball3 = self.childNode(withName: "ball3") as! SKSpriteNode
    ball4 = self.childNode(withName: "ball4") as! SKSpriteNode
    motionManager = CMMotionManager()
    motionManager.startAccelerometerUpdates()
    let border = SKPhysicsBody(edgeLoopFrom: self.frame)
    border.friction = 0
    border.restitution = 0
    self.physicsBody = border
}


override func update(_ currentTime: TimeInterval) {
    if let accelerometerData = motionManager.accelerometerData {
        physicsWorld.gravity = CGVector(dx: accelerometerData.acceleration.x * 20, dy: accelerometerData.acceleration.y * 35)
    }
}

}

聚苯乙烯。我应该在协议函数goback2中添加什么来加载主视图控制器?

0 个答案:

没有答案