我正在开发一种船和石头相撞的游戏。游戏结束了当船碰撞石头时。我为船和石头的交叉实现了intersects()方法。对于我收集的每枚硬币,我将分数增加+1。当船和石头碰撞时,我正在表演。执行segue并使用重启选项显示游戏分数。一切顺利,直到我重新开始游戏,当比赛重新开始时,有一些问题。以下是问题:
首先游戏很顺利,GameOver控制器会在碰撞发生时显示,但是当我选择重新启动时。
问题#1当船撞到石头时,segue显示两次,然后再次重新启动
问题#2当我第三次玩游戏时,它没有检测到交叉点并且它正在抛出错误
以下是错误的屏幕截图。
我可以第一次玩游戏,但是当我重新启动时我无法玩游戏。重启时游戏无法正常运行。
当使用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)
}
这是GameOver.swift,它有一个重启按钮,实现了对前一个控制器的segue。
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时。
任何建议都是有价值的。
谢谢。
答案 0 :(得分:1)
从你的代码我可以看到你从ViewController.swift触发segue到GameOver.swift这是好的,这意味着你正在呈现GameOverViewController。问题是你不应该从GameOverViewController回到你的ViewController.swift,你必须解雇你的gameOverViewController
在重启功能中
@IBAction func restartGame(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
我能看到的第二个问题是你是否正在试图从准备segue中调用一个函数
您不应该将此segue用于ViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goBack" {
if let back = segue.destination as? ViewController {
// this is wrong
back.startGame()
}
}
}
您可以使用unwindSegue,delegate,notification ...来触发startGame()函数