在CGRectIntersectsRect中准备好

时间:2017-04-18 19:45:15

标签: ios swift

我差不多完成了一场比赛。游戏包括一艘移动的船,我们需要避免移动石头。当船和石头碰撞时游戏结束。我想实现第二个视图控制器,它显示游戏结束标签,游戏分数和重启按钮。 GameOver.swift是另一个具有游戏分数和重启选项的viewcontroller。

这是我的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.
    }
}

2 个答案:

答案 0 :(得分:1)

ViewController的prepare(forSegue:)不是您调用的函数。它就是在你转向新的ViewController之前召唤你的。为了显示您的辅助ViewController,您将不得不创建它的实例,然后自己触发segue。然后,您将设置prepare()函数,以允许主viewController将数据传递给辅助视图。

以下是完成此项工作所需的步骤:

  1. 在Interface Builder中创建GameOver ViewController的实例。
  2. 从您的第一个ViewController创建一个手动segue,并为其指定一个标识符。
  3. performSegue(withIdentifier:)
  4. 中致电StopGame()
  5. 如果需要,请覆盖主要ViewController的{​​{1}},以便将数据(例如得分)传递给prepare(for segue:) GameOver

答案 1 :(得分:0)

如果您想使用segue呈现新的视图控制器,请致电performSegue(withIdentifier: "YourSegueIdentifier", sender: self)

通过调用此函数触发

prepare