2017-04-18 52 views
0

我幾乎完成了我的遊戲。遊戲由一艘移動的小船組成,我們需要避免移動石塊。當船和石頭碰撞時,遊戲結束。我想實現第二個視圖控制器,它顯示gameover標籤,遊戲分數和重啓按鈕。 GameOver.swift是另一款帶有遊戲分數和重新啓動選項的視圖控制器。prepareforsegue inside CGRectIntersectsRect

這裏是我的ViewController.swift

import UIKit 
import QuartzCore 

class ViewController: UIViewController { 
    @IBOutlet weak var myLBL: UILabel! 
    @IBOutlet weak var myView: UIView! 
    @IBOutlet weak var collectedCoin: UILabel! 
    // weak var moveWater: MovingWater! 

    var views : [String : UIView]! 

    var boat:UIImageView! 
    var stone:UIImageView! 
    var food:UIImageView! 
    var boatWreck:UIImageView! 
    var boatLeftRight : UILongPressGestureRecognizer! 

    var coins = Int() 
    var pass = "HELLO" 

    var tapTimer:Timer! 
    var tapTimer2: Timer! 

    var leftM:UInt32 = 55 
    var rightM:UInt32 = 250 

    var leftS:UInt32 = 35 
    var rightS:UInt32 = 220 

    func startGame() { 
     boat = UIImageView(image: UIImage(named: "boat")) 
     boat.frame = CGRect(x: 0, y: 0, width: 60, height: 90) 
     boat.frame.origin.y = self.view.bounds.height - boat.frame.size.height - 10 
     boat.center.x = self.view.bounds.midX 

     self.view.addSubview(boat) 

     boatLeftRight = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.leftRight(tap:))) 
     boatLeftRight.minimumPressDuration = 0.001 
     myView.addGestureRecognizer(boatLeftRight) 

     tapTimer2 = Timer.scheduledTimer(timeInterval: TimeInterval(0.05), target: self, selector: #selector(ViewController.change), userInfo: nil, repeats: true) 
    } 

    func leftRight(tap:UILongPressGestureRecognizer) { 
     if tap.state == UIGestureRecognizerState.ended { 
      if (tapTimer != nil) { 
       self.tapTimer.invalidate() 
      } 
     } else if tap.state == UIGestureRecognizerState.began { 
      let touch = tap.location(in: myView) 
      if touch.x > myView.frame.midX { 
       tapTimer = Timer.scheduledTimer(timeInterval: TimeInterval(0.005), target: self, selector: #selector(ViewController.moveBoat(time:)), userInfo: "right", repeats: true) 

      } else { 
       tapTimer = Timer.scheduledTimer(timeInterval: TimeInterval(0.005), target: self, selector: #selector(ViewController.moveBoat(time:)), userInfo: "left", repeats: true) 
      } 
     } 
    } 

    func moveBoat(time:Timer) { 

     if let d = time.userInfo as? String! { 
      var bot2 = boat.frame 

      if d == "right" { 
       if bot2.origin.x < CGFloat(rightM) { 
        bot2.origin.x += 2 
       } 
      } else { 
       if bot2.origin.x > CGFloat(leftM) { 
        bot2.origin.x -= 2 
       } 
      } 
      boat.frame = bot2 
     } 
    } 

    func movingStone() { 
     stone = UIImageView(image: UIImage(named: "stones.png")) 
     stone.frame = CGRect(x: 0, y: 0, width: 60, height: 90) 
     var stone2 = leftS + arc4random() % rightS 

     stone.bounds = CGRect(x:10, y:10, width:81.0, height:124.0) 
     stone.contentMode = .center; 
     stone.layer.position = CGPoint(x: Int(stone2), y: 10) 
     stone.transform = CGAffineTransform(rotationAngle: 3.142) 

     self.view.insertSubview(stone, aboveSubview: myView) 

     UIView.animate(withDuration: 5, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {() -> Void in 
      self.stone.frame.origin.y = self.view.bounds.height + self.stone.frame.height + 10 
     }) { (success:Bool) -> Void in 

      self.stone.removeFromSuperview() 
      self.movingStone() 

     } 
    } 

    func movingFood() { 
     food = UIImageView(image: UIImage(named: "fishcoin2.png")) 

     var stone3 = leftS + arc4random() % rightS 

     food.bounds = CGRect(x:0, y:0, width:81.0, height:124.0) 
     food.contentMode = .center; 
     food.layer.position = CGPoint(x: Int(stone3), y: 40) 
     food.transform = CGAffineTransform(rotationAngle: 3.142) 

     self.view.insertSubview(food, aboveSubview: myView) 

     UIView.animate(withDuration: 5, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {() -> Void in 
      self.food.frame.origin.y = self.view.bounds.height + self.food.frame.height - 50 
     }) { (success:Bool) -> Void in 

      self.food.removeFromSuperview() 
      self.movingFood() 

     } 
    } 

    func change(tap2: Timer) { 
     if(boat.layer.presentation()?.frame.intersects((food.layer.presentation()?.frame)!))! 
     { 
      coins = coins + 1 
      collectedCoin.text = "\(coins)" 

      if coins > 100 { 

       let a = UIAlertController(title: "WON", message: "WANT AGAIN", preferredStyle: UIAlertControllerStyle.alert) 

       a.addAction(UIAlertAction(title: "A", style: UIAlertActionStyle.cancel, handler: nil)) 

       a.addAction(UIAlertAction(title: "B", style: UIAlertActionStyle.default, handler: { (a:UIAlertAction!) -> Void in 
        self.startGame() 
       })) 
       self.present(a, animated: true, completion: nil) 
      } 
     } 

     else if(boat.layer.presentation()?.frame.intersects((stone.layer.presentation()?.frame)!))! { 

      //this is where boat and stone collide 
      //I want to implement the gameoverVC here 
      //prepare() is not code sensing in my Xcode 
      stopGame() 
     } 
    } 

    func stopGame() { 
     tapTimer2.invalidate() 

     boat.image = UIImage(named: "wreckboat.png") 

     self.stone.layer.removeAllAnimations() 
     self.food.layer.removeAllAnimations() 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     //moveWater.backgroundStart() 
     startGame() 
     movingStone() 
     movingFood() 
     coins = 10 
     collectedCoin.text = "\(coins)" 
    } 

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

那麼你的問題是什麼? –

回答

1

ViewController的prepare(forSegue:)不是您調用的函數。這是要求你,在繼續前進到一個新的ViewController。爲了顯示你的第二個ViewController,你將不得不創建它的一個實例,然後自己觸發segue。然後,您將設置prepare()函數以允許您的主視圖控制器將數據傳遞給輔助視圖。

下面是你需要做這項工作的步驟:

  1. 創建您在接口GameOverViewController生成器的實例。
  2. 從您的第一個ViewController創建一個手動segue併爲其指定一個標識符。
  3. 致電performSegue(withIdentifier:)在您的StopGame()
  4. 如果需要,請覆蓋主要的ViewControllerprepare(for segue:)以將數據(例如分數)傳遞給GameOverViewController
+0

我在GameOver VC中獲得了分數,但問題在於當船和硬幣相交時,我正在增加分數,我正在將其增加1。但是,它會增加28 56 ...原因是什麼? –

+0

@PrasannaVigneshwar你正在增加船上和船上硬幣相交的每個框架上的'硬幣',所以如果船上的硬幣碰到30幀,它就會增加30倍。爲了解決這個問題,你可以把它作爲一個單獨的問題來提出。 – Robert

0

如果你想使用SEGUE提出一個新的視圖控制器,你應該叫performSegue(withIdentifier: "YourSegueIdentifier", sender: self)。通過調用這個函數來觸發prepare