2017-04-21 66 views
0

我正在开发一个游戏,船和石头相撞。当船碰撞石头时,游戏结束。我为船和石头的交点实施了intersects()方法。对于我收集的每一枚硬币,我将分数增加+1。当船和石头相撞时,我正在进行一次轮回。执行赛格并使用重新启动选项显示比赛分数。一切都很顺利,直到我重新开始游戏,当游戏重新开始时,有一些问题。以下是问题:使用segues实现游戏,重新启动时崩溃。斯威夫特

起初游戏非常流畅,GameOver控制器在碰撞发生时显示,但是当我选择重新启动时显示。

问题1当船碰撞的石,SEGUE显示两次,并再次重新启动后

问题#2当我在玩第3次在没有检测的交集,它抛出一个错误

这是错误的屏幕快照。

enter image description here我可以第一次玩游戏,但重新开始时我无法玩游戏。重新启动时游戏无法正常工作。

这发生在使用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。

任何建议将是有价值的。

谢谢。

回答

1

从你的代码我可以看到你从ViewController.swift触发了一个从GameOver.swift继承的继承,这是好的,这意味着你正在呈现GameOverViewController。问题是你不应该GameOverViewController Segue公司回到你ViewController.swift你必须要关闭重启功能

@IBAction func restartGame(_ sender: Any) { 
    self.dismiss(animated: true, completion: nil) 
} 

第二个问题,我可以看到里面的gameOverViewController

是你试图调用从功能赛格瑞

你不应该使用这个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()函数

+0

hI会解决这个问题,但为什么当我按下重新启动时,segue会执行两次,即segue到GameOverVC?并在下一次抛出图像中的错误。是因为在GameOver中玩游戏吗? –

+0

所以你的意思是在segue的body中错误地调用了startGame()方法?那么,这会让代码崩溃? –

+0

我不得不放松,但如何调用startGame() 我做了 performSegue(withIdentifier:“unwindto”,sender:self) –