2016-12-27 48 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