我正在開發一個遊戲,船和石頭相撞。當船碰撞石頭時,遊戲結束。我爲船和石頭的交點實施了intersects()方法。對於我收集的每一枚硬幣,我將分數增加+1。當船和石頭相撞時,我正在進行一次輪迴。執行賽格並使用重新啓動選項顯示比賽分數。一切都很順利,直到我重新開始遊戲,當遊戲重新開始時,有一些問題。以下是問題:使用segues實現遊戲,重新啓動時崩潰。斯威夫特
起初遊戲非常流暢,GameOver控制器在碰撞發生時顯示,但是當我選擇重新啓動時顯示。
問題1當船碰撞的石,SEGUE顯示兩次,並再次重新啓動後
問題#2當我在玩第3次在沒有檢測的交集,它拋出一個錯誤
這是錯誤的屏幕快照。
我可以第一次玩遊戲,但重新開始時我無法玩遊戲。重新啓動時遊戲無法正常工作。
這發生在使用segues時,當我使用UIAlertView遊戲運行正常時。我想執行segues。
這裏是ViewController.swift
func movingStone() {
stone = UIImageView(image: UIImage(named: "stones.png"))
stone.frame = CGRect(x: 0, y: 0, width: 5.0, height: 5.0)
stone.bounds = CGRect(x:0, y:0, width: 5.0, height:5.0)
stone.contentMode = .center;
stone.layer.position = CGPoint(x: boat.center.x - 5, y: 0.0)
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"))
food.frame = CGRect(x: 0, y: 0, width: 5.0, height: 5.0)
var stone3 = leftS + arc4random() % rightS
food.bounds = CGRect(x:0, y:0, width: 5.0, height: 5.0)
food.contentMode = .center;
food.layer.position = CGPoint(x: boat.center.x + 20, y: 0.0)
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 intersectsAt(tap2 : Timer) {
if(boat.layer.presentation()?.frame.intersects((food.layer.presentation()?.frame)!))!{
food.layer.isHidden = true
coins = +1
collectedCoin.text = "\(coins)"
if coins > 100 {
super.performSegue(withIdentifier: "GameOverIdentifier", sender: self)
}
}
if(boat.layer.presentation()?.frame.intersects((stone.layer.presentation()?.frame)!))! {
stopGame()
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "GameOverIdentifier" {
let gameOver = segue.destination as? GameOver
gameOver?.coin = coins
}
}
func stopGame() {
tapTimer2.invalidate()
boat.image = UIImage(named: "wreckboat.png")
super.performSegue(withIdentifier: "GameOverIdentifier", sender: self)
}
這裏是一個具有重新啓動按鈕,實現SEGUE以前控制器GameOver.swift。
import UIKit
class GameOver: UIViewController {
var coin = Int()
var restart : ViewController!
@IBOutlet weak var go: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
go.text = "\(self.coin)"
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func restartGame(_ sender: Any) {
performSegue(withIdentifier: "goBack", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goBack" {
if let back = segue.destination as? ViewController {
back.startGame()
}
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
我認爲這個問題是當我使用的touchesBegan和touchesMoved。
任何建議將是有價值的。
謝謝。
hI會解決這個問題,但爲什麼當我按下重新啓動時,segue會執行兩次,即segue到GameOverVC?並在下一次拋出圖像中的錯誤。是因爲在GameOver中玩遊戲嗎? –
所以你的意思是在segue的body中錯誤地調用了startGame()方法?那麼,這會讓代碼崩潰? –
我不得不放鬆,但如何調用startGame() 我做了 performSegue(withIdentifier:「unwindto」,sender:self) –