我正在开发一个游戏,船和石头相撞。当船碰撞石头时,游戏结束。我为船和石头的交点实施了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) –