gpt4 book ai didi

ios - CGRectIntersectsRect 中的 prepareforsegue

转载 作者:行者123 更新时间:2023-11-28 15:43:04 24 4
gpt4 key购买 nike

我的游戏快完成了。游戏由一艘移动的船组成,我们需要避免移动石头。当船和石头相撞时,游戏结束。我想实现第二个 View Controller ,它显示游戏结束标签、游戏得分和重启按钮。GameOver.swift 是另一个具有游戏得分和重启选项的 View Controller 。

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

最佳答案

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

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

  1. 在 Interface Builder 中创建 GameOver ViewController 的实例。
  2. 从您的第一个 ViewController 创建一个到它的手动 segue 并给它一个标识符。
  3. 在您的 StopGame() 中调用 performSegue(withIdentifier:)
  4. 如果需要,覆盖主 ViewControllerprepare(for segue:) 以将数据(例如分数)传递给 GameOver View Controller

关于ios - CGRectIntersectsRect 中的 prepareforsegue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43480902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com