2016-12-27 49 views
-1

确定有错误,我解决了,谢谢你们帮助,但现在统一继续挤压当我按下运行按钮很多人说因为循环,但我真的不知道如何修复它统一保持崩溃

GameManager.cd

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 
using UnityEngine.SceneManagement; 
using System.Collections.Generic; 

public class GameManager : MonoBehaviour { 

public Sprite[] cardFace; 
public Sprite cardBack; 
public GameObject[] cards; 
public Text macthText; 

private bool _init = false; 
private int _matches = 13; 



// Update is called once per frame 
void Update() { 


    if (!_init) 
     initializecards(); 

    if (Input.GetMouseButtonUp (0)) 
     checkCards(); 

} 

void initializecards(){ 

    for (int id = 0; id < 2; id++) { 
     for (int i = 1; i < 14; i++) { 
      bool test = false; 
      int choice = 0; 
      while (!test) { 
       choice = Random.Range (0, cards.Length); 
       test = !(cards [choice].GetComponent<Card>().Initialized); 
      } 
      cards [choice].GetComponent<Card>().CardValue = i; 
      cards [choice].GetComponent<Card>().Initialized = true; 
     } 
    } 

    foreach (GameObject c in cards) 
     c.GetComponent<Card>().setupGrapgics(); 

    if (!_init) 
     _init = true; 

} 

public Sprite getCardBack(){ 
    return cardBack; 
    } 

public Sprite getCardface(int i){ 

    return cardFace [i-1]; 
} 

public void checkCards() 
{ 
    List<int> c = new List<int>(); 

    for (int i = 0; i > cards.Length; i++) { 

     if (cards [i].GetComponent<Card>().State == 1) 
      c.Add (i); 
    } 

    if (c.Count == 2) 
     cardComparison (c); 
} 

void cardComparison(List<int> c){ 
    Card.DO_NOT = true; 

    int x = 0; 

    if (cards [c [0]].GetComponent<Card>().CardValue == cards [c [1]].GetComponent<Card>().CardValue) { 
     x = 2; 
     _matches--; 
     macthText.text = "Number of Macthes: " + _matches; 
     if(_matches == 0) 
      SceneManager.LoadScene("Menu"); 

    } 

    for(int i = 0; i < c.Count; i++){ 

     cards [c [i]].GetComponent<Card>().State = x; 
     cards [c [i]].GetComponent<Card>().falseCheck(); 

    } 


    } 
    } 

card.cd

using UnityEngine; 
using UnityEngine.UI; 
using System.Collections; 

public class Card : MonoBehaviour { 

public static bool DO_NOT = false; 

[SerializeField] 
private int state; 
[SerializeField] 
private int cardValue; 
[SerializeField] 
private bool initialized = false; 

private Sprite cardBack; 
private Sprite cardFace; 

private GameObject manager; 

void start() { 
    state = 0; 
    manager = GameObject.FindGameObjectWithTag ("Manager"); 

} 

public void setupGrapgics() { 

    cardBack = manager.GetComponent<GameManager>().getCardBack(); 
    cardFace = manager.GetComponent<GameManager>().getCardface  (cardValue); 

    flipCard(); 

} 

public void flipCard() { 

    if(state == 0 && !DO_NOT) 
     GetComponent<Image>().sprite = cardBack; 
    else if (state == 1 && !DO_NOT) 
     GetComponent<Image>().sprite = cardFace; 

} 

public int CardValue { 

    get { return cardValue;} 
    set { cardValue = value; } 


} 

public int State { 

    get { return state; } 
    set { state = value; } 
} 

public bool Initialized { 

    get { return initialized; } 
    set { Initialized = value; } 

} 

public void falseCheck(){ 

    StartCoroutine (pause()); 

} 

IEnumerator pause() { 

    yield return new WaitForSeconds (1); 
    if (state == 0) 
     GetComponent<Image>().sprite = cardBack; 
    else if (state == 1) 
     GetComponent<Image>().sprite = cardFace; 
    DO_NOT = false; 

} 
} 

在先进的感谢

+0

请阅读[问],参加[旅游],当然[MCVE] – Plutonix

+0

你的意思是“崩溃”? –

回答

0

下面的代码冻结的游戏,因为test总是false永不true

while (!test) { 
choice = Random.Range (0, cards.Length); 
test = !(cards [choice].GetComponent<Card>().Initialized); 
} 

跟踪它到:

public bool Initialized { 
    get { return initialized; } 
    set { Initialized = value; } 
} 

因为set { Initialized = value; }应该是set { initialized = value; }

注:

在使用生成的数字作为变量退出循环不产生在一个while循环随机数。如果失败了,你会冻结Unity。在协程函数中这样做会很好,然后把yield return null;放在最后。那永远不会冻结Unity。

while (!test) { 
choice = Random.Range (0, cards.Length); 
test = !(cards [choice].GetComponent<Card>().Initialized); 
yield return null; 
} 
+0

但现在我有这个错误 –

+0

资产/脚本/ GameManager.cs(31,14):错误CS1624:'GameManager.initializecards()'的主体不能是迭代块,因为'void'不是迭代器接口类型 –

+1

就像我在回答的最后一节所说的,你必须使它成为一个协同程序功能。 'void initializecards()'应该是'IEnumerator initializecards()'。不要试图从答案中复制代码,阅读它所说的内容。无论你调用'initializecards();'你现在都必须用'StartCoroutine(initializecards());'调用它。对于[最后一个问题](http://stackoverflow.com/q/41326049/3785314),您问,如果他们解决了您的问题,您可以接受其中一个答案。 – Programmer