2016-05-10 179 views
0

如何使用協議?我無法使用它。當我點擊開始按鈕時,我只想打印出「abc」。它沒有工作。 plz幫助我如何在uiviewcontroller中使用協議,swift

import UIKit 
import SpriteKit 


protocol GameDelegate { 
    func gameOver() 
} 

class MainMenuScene: SKScene 
{ 
    var Gdelegate: GameDelegate? 

    override func didMoveToView(view: SKView) 
    { 
     let background = SKSpriteNode(imageNamed: "DiscsBackground") 
     background.size = self.size 
     background.zPosition = 0 
     background.position = CGPoint(x: self.size.width/2, y: self.size.height/2) 
     self.addChild(background) 

     let gameTitleLabel1 = SKLabelNode(fontNamed: "Pusab") 
     gameTitleLabel1.text = "Disappearing" 
     gameTitleLabel1.fontSize = 90 
     gameTitleLabel1.fontColor = SKColor.whiteColor() 
     gameTitleLabel1.position = CGPoint(x: self.size.width/2, y: self.size.height*0.75) 
     gameTitleLabel1.zPosition = 1 
     self.addChild(gameTitleLabel1) 

     let gameTitleLabel2 = SKLabelNode(fontNamed: "Pusab") 
     gameTitleLabel2.text = "Discs" 
     gameTitleLabel2.fontSize = 250 
     gameTitleLabel2.fontColor = SKColor.whiteColor() 
     gameTitleLabel2.position = CGPoint(x: self.size.width/2, y: self.size.height*0.6) 
     gameTitleLabel2.zPosition = 1 
     self.addChild(gameTitleLabel2) 

     let gameByLabel = SKLabelNode(fontNamed: "Pusab") 
     gameByLabel.text = "Disappearing" 
     gameByLabel.fontSize = 90 
     gameByLabel.fontColor = SKColor.whiteColor() 
     gameTitleLabel2.position = CGPoint(x: self.size.width/2, y: self.size.height*0.95) 
     gameByLabel.zPosition = 1 
     self.addChild(gameByLabel) 


     let startLabel = SKLabelNode(fontNamed: "Pusab") 
     startLabel.text = "start" 
     startLabel.fontSize = 150 
     startLabel.fontColor = SKColor.whiteColor() 
     startLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.35) 
     startLabel.zPosition = 1 
     startLabel.name = "startButton" 
     self.addChild(startLabel) 
    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) 
    { 
     for touch: AnyObject in touches 
     { 

      let pointOfTouch = touch.locationInNode(self) 
      let tappedNode = nodeAtPoint(pointOfTouch) 
      let tappedNodeName = tappedNode.name 

      if tappedNodeName == "startButton" 
      { 
       //self.scene?.view?.presentScene(nil) 
       Gdelegate?.gameOver() 
      } 
     } 


    } 

} 

class MHAGameViewController: UIViewController,GameDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let scene = MainMenuScene(size: CGSize(width: 1536, height: 2048)) 
     // Configure the view. 
     let skView = self.view as! SKView 
     skView.showsFPS = true 
     skView.showsNodeCount = true 

     /* Sprite Kit applies additional optimizations to improve rendering performance */ 
     skView.ignoresSiblingOrder = true 

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

     skView.presentScene(scene) 

    } 

    func gameOver() {/* 
     self.performSegueWithIdentifier("segue1", sender: self) 
     let alertController = UIAlertController(title: "HELP", message: 
      "Please wait....", preferredStyle: UIAlertControllerStyle.Alert) 
     alertController.addAction(UIAlertAction(title: "close", style: UIAlertActionStyle.Default,handler: nil)) 

     self.presentViewController(alertController, animated: true, completion: nil)*/ 

     print("abc") 

    } 


    override func shouldAutorotate() -> Bool { 
     return true 
    } 

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { 
     if UIDevice.currentDevice().userInterfaceIdiom == .Phone { 
      return .AllButUpsideDown 
     } else { 
      return .All 
     } 
    } 

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

    override func prefersStatusBarHidden() -> Bool { 
     return true 
    } 
} 

回答

1

你應該設置委託如下:

let scene = MainMenuScene(size: CGSize(width: 1536, height: 2048)) 
scene.Gdelegate = self