2017-04-21 1 views
0

私はボートと石が衝突するゲームを開発しています。ゲームが終わったときボートが石に衝突したとき。私は、ボートと石の交差点のためのintersects()メソッドを実装しました。私が集めたコインごとに、スコアを+1します。私はボートと石の両方が衝突したときにセグを実行しています。セグが実行され、ゲームのスコアが再起動オプションと共に表示されます。ゲームを再開するまではすべてがスムーズで、ゲームが再開されるといくつかの問題があります。次の問題があります。セグを使用してゲームを実装し、再起動するとクラッシュする。 Swift

最初はゲームがスムーズで、衝突が発生したときに再起動を選択したときにGameOverコントローラが表示されます。ボートは石を衝突すると

問題#1、セグエは二回して、もう一度、私はそれが交差点を検出していない第三の時間のために遊んでいますし、それがエラーを投げている

問題#2を再​​起動した後に表示されます

ここにエラーのスクリーンショットがあります。

enter image description here私は初めてゲームをすることができますが、再起動するとゲームをプレイできません。再起動するとゲームが正しく動作しません。

これは、私がUIAlertViewを使用しているときに、セグが使用されているときに起こります。ゲームはスムーズに動作しています。私はセグを実装したい。ここ

はここ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です。

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
私はあなたがokですGameOver.swiftへViewController.swiftからセグエを誘発することを確認することができますどのようなあなたのコードから

、それはあなたがGameOverViewControllerを提示していることを意味します。問題は、あなたがあなたの再始動機能

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

私が見ることができる第二の問題の内側にあなたのgameOverViewController

を却下しなければならないあなたのViewController.swiftに戻っGameOverViewControllerからセグエないはずであるあなたは、呼び出ししようとしていますあなたからの関数はあなたがあなたの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() 
    } 

    } 
} 

にこのセグエをバック使うべきではありません

セグエの準備ますHIはそれを修正します

+0

()関数で、あなたのstartGameをトリガするために... unwindSegue、委任、通知を使用しますが、なぜ私が押したときにセグエがGameOverVCに二回、すなわちセグエを行って再起動することができますか?次に画像にあるエラーを投げています。それはGameOverのセグのためですか? –

+0

これは、segueの本体でstartGame()メソッドが間違って呼び出されたことを意味しますか?だから、クラッシュするコードを作っていますか? –

+0

私はリラックスするために持っているが、startGame()を呼び出す方法を は私が performSegue(withIdentifier:「unwindto」、送信者:自己を)でしたあなたはprepareForSegueからstartGame()を呼び出すとき –

関連する問題