我幾乎完成了我的遊戲。遊戲由一艘移動的小船組成,我們需要避免移動石塊。當船和石頭碰撞時,遊戲結束。我想實現第二個視圖控制器,它顯示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.
}
}
那麼你的問題是什麼? –