2015-09-03 49 views
0

Reloading bar problemUnity2D C#重裝進度條不工作propertly

所以我做一個自上而下的坦克射擊遊戲,我想做出比以前更好的重裝系統。所以我認識到我需要一些進度條的國王。我知道如何做到這一點,所以我開始這樣做。問題是它不能正常工作。正如我在上面的.gif中顯示的,當你第二次拍攝時,進度條不會下降。因爲我對團結感很陌生,所以我還是不太瞭解所有事情。所以我來到這裏,也許有人可以幫忙。

編輯: 我剛剛發現了另一個問題,也許一個答案,爲什麼我有這個問題。我的腳本試圖重新加載第二次,我的「needTimer」布爾是假的,因此進度條是不會下來的時候它是假的。新的問題將會是爲什麼它變得虛假而不是真實? 我重裝腳本:

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

public class Reload : MonoBehaviour { 

    public float ammo; 
    public Image progress; 
    public bool alreadyReloading; 
    public bool needTimer; 

    void Start() { 
     alreadyReloading = false; 
    } 
    IEnumerator needtimertime(){ 
     yield return new WaitForSeconds (6.5f); 
     needTimer = false; 
    } 
    IEnumerator uztaisyt(){ 
     Debug.Log ("REEELOUUUDING!"); 
     yield return new WaitForSeconds(6.5f); 
     ammo += 1; 
     alreadyReloading = false; 
    } 

    void Update() { 
     if (needTimer == true) { 
      timer (""); 
     } 
     if (ammo < 5) { 
      if(alreadyReloading == false){ 
       needTimer = true; 
       StartCoroutine(uztaisyt()); 
       alreadyReloading = true; 
      } 
     } 
     if (progress.fillAmount <= 0) { 
      progress.fillAmount = 1.0f; 
     } 
    } 

    void timer(string tipas){ 
     progress.fillAmount -= Time.deltaTime/6.5f; 
     StartCoroutine (needtimertime()); 
    } 
} 
+0

是什麼,當你試圖拍攝第二次彈藥變量的值? –

+0

@Pawel Marecki 4,每次拍攝時都會下降。它顯示在gif – Justasmig

+0

請澄清:_alreadyReloading_和_needTimer_是公開的,它們是否從場景中的任何其他對象被調用?另外,_ammo_字段在哪裏以及如何遞減?如果_ammo_遞減的時間與Reload Update()函數不同步,那可能是一個可能的罪魁禍首。 – BgRva

回答

0

我發現我的問題,並修復它。 問題是needTimer變得虛假。所以我找到了它並將其刪除。

我的新代碼:

using UnityEngine; 

使用UnityEngine.UI;使用System.Collections的 ;

公共類刷新:MonoBehaviour {

public float ammo; 
public Image progress; 
public bool alreadyReloading; 
public bool needTimer; 

void Start() { 
    alreadyReloading = false; 
    needTimer = false; 
} 

IEnumerator uztaisyt(){ 
    Debug.Log ("REEELOUUUDING!"); 
    yield return new WaitForSeconds(6.5f); 
    ammo += 1; 
    alreadyReloading = false; 
} 

void Update() { 
    if (ammo < 5.0f) { 
     if(alreadyReloading == false){ 
      progress.fillAmount = 1.0f; 
      needTimer = true; 
      StartCoroutine(uztaisyt()); 
      alreadyReloading = true; 
     } 
     if (needTimer == true) { 
      timer (""); 
     } 
    } 
} 

void timer(string tipas){ 
    progress.fillAmount -= Time.deltaTime/6.5f; 
} 

}

0

如果您在拍攝的第一時間(動畫)後啓動uztaisyt()作爲協程,needTimer設置爲true,並在下一個更新()調用,needtimertime()協同程序將啓動。由於兩個uztaisyt()needtimertime()具有相同6.5秒等待,他們會在同一幀更新,因爲needtimertime()總是會在下一幀之後uztaisyt開始都返回( )。而且,由於沒有更新()調用之間的時間間隔的保證,(見Time and Frame Managment),這個時間間隔可能會超過預期,needtimertime()可能之後uztaisyt返回幀錯誤()在第二次射擊後被調用。

爲了確保needtimertime()總是啓動(如果尚未運行),緊隨其後的呼籲uztaisyt()(並呼籲在同一幀更新中),你可以試試下面的更新刷新腳本(基本上更改爲Update()方法以及何時/如何設置_isTimerRunning)。

public class Reload : MonoBehaviour { 

    public float ammo; 
    public Image progress; 

    private bool _alreadyReloading; 
    private bool _isTimerRunning; 

    void Start() { 
     _alreadyReloading = false; 
     _isTimerRunning = false; 
    } 

    IEnumerator needtimertime(){ 
     yield return new WaitForSeconds (6.5f); 
     _needTimer = false; 
    } 

    IEnumerator uztaisyt(){ 
     Debug.Log ("REEELOUUUDING!"); 
     yield return new WaitForSeconds(6.5f); 
     ammo += 1; 
     _alreadyReloading = false; 
    } 

    void Update() { 
     if (ammo < 5) { 
      if(_alreadyReloading == false){           
       StartCoroutine(uztaisyt()); 
       _alreadyReloading = true; 

       //this will check for and start the progress bar timer in the same udate call 
       //so both coroutines finish on the same frame update 
       if(!_isTimerRunning){ 
       _isTimerRunning = true; 
       timer (""); 
       } 
      } 
     } 
     if (progress.fillAmount <= 0) { 
      progress.fillAmount = 1.0f; 
     } 
    } 

    void timer(string tipas){ 
     progress.fillAmount -= Time.deltaTime/6.5f; 
     StartCoroutine (needtimertime()); 
    } 
} 
+0

我發現這種方法不起作用,因爲(我認爲,我的看法)_timer()_只被調用一次,沒有給進度條足夠的時間,所以不行。 (而不是下降它獲得值0.9967943並停止)。我認爲它需要像之前調用的那樣調用(if(needTimer == true){timer(「」);}),但是使用這種時間方法來同步開始時間。 – Justasmig