Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CGRectIntersectsRect内的prepareforsegue_Ios_Swift - Fatal编程技术网

Ios CGRectIntersectsRect内的prepareforsegue

Ios CGRectIntersectsRect内的prepareforsegue,ios,swift,Ios,Swift,我几乎完成了我的a游戏。游戏由移动的船组成,我们需要避免移动石头。当船和石头相撞时,游戏结束。我想实现第二个视图控制器,它显示gameover标签、游戏分数和重新启动按钮。 GameOver.swift是另一个具有游戏分数和重新启动选项的viewcontroller 这是我的ViewController.swift import UIKit import QuartzCore class ViewController: UIViewController { @IBOutlet weak

我几乎完成了我的a游戏。游戏由移动的船组成,我们需要避免移动石头。当船和石头相撞时,游戏结束。我想实现第二个视图控制器,它显示gameover标签、游戏分数和重新启动按钮。 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.
    }
}

如果要使用segue显示新的视图控制器,应调用performsguewithidentifier:YourSegueIdentifier,sender:self

调用此函数将触发prepare。

ViewController的PrepareForegue:不是您调用的函数。在切换到新的ViewController之前,会为您调用它。为了显示辅助ViewController,您必须创建它的一个实例,然后自己触发segue。然后设置prepare函数,以允许主viewController将数据传递给次viewController

以下是实现此功能所需的步骤:

在Interface Builder中创建GameOver ViewController的实例。 从第一个ViewController创建到它的手动序列,并为它指定一个标识符。 在StopGame中调用performsguewithidentifier:。 如果需要,覆盖主ViewController的preparefor segue:以将分数等数据传递给GameOver ViewController。
那么你的问题是什么呢?在GameOver VC中,我得到了分数,但问题是当船和硬币相交时,我增加了分数,我增加了1。然而,它增加了28 56。。。原因是什么?@PrasannaVigneshwar你在船和硬币相交的每一帧上增加硬币,因此如果船接触硬币30帧,它将增加30倍。对于解决这个问题的方法,你可以作为一个单独的问题来提问。