2016-07-28 2 views
0

플레이어의 현재 상태가 0에 도달하면 타이머 카운트 다운을 시작하고 싶습니다. 지금까지 타이머가 내 게임과 연결되어 있는데, 그의 마음 (삶)의. 응용 프로그램 (게임)을 실행하면 타이머가 자동으로 시작되고 패널 위로 게임이 끝나면 타이머가 멈 춥니 다. 그건 내가 원하는 것이 아니야. 내가하고 싶은 일은 플레이어가 모든 하트 (생명)를 잃고 패널 위로 게임이 끝날 때 타이머를 시작하는 것입니다. '당신은 깨어() 및 업데이트에 코드를() 메소드하지만 난 돈Unity 2D : 플레이어가 모든 생명을 잃을 때 타이머를 시작하는 방법

public Text timer; 
int minutes = 1; 
int seconds = 0; 
float miliseconds = 0; 

public int curHealth; 
public int maxHealth = 3; 

[Range(1, 59)] 
public int defaultStartMinutes = 1; 
public bool allowTimerRestart = false; 
public bool useElapsedTime = true; 

private int savedSeconds; 
private bool resetTimer = false; 

private DateTime centuryBegin = new DateTime(2001, 1, 1); 

void Start() 
{ 
    curHealth = maxHealth; 
} 

void Awake() 
{ 
    minutes = defaultStartMinutes; 
    if (PlayerPrefs.HasKey("TimeOnExit")) 
    { 
     miliseconds = PlayerPrefs.GetFloat("TimeOnExit"); 
     savedSeconds = (int)miliseconds; 

     if (useElapsedTime && PlayerPrefs.HasKey("CurrentTime")) 
     { 
      int elapsedTicks = (int)(DateTime.Now.Ticks/10000000); 
      int ct = PlayerPrefs.GetInt("CurrentTime", elapsedTicks); 
      PlayerPrefs.DeleteKey("CurrentTime"); 
      elapsedTicks -= ct; 
      if (elapsedTicks < miliseconds) 
      { 
       miliseconds -= elapsedTicks; 
      } 
      else 
      { 
       miliseconds = 0; 
      } 
     } 

     minutes = (int)miliseconds/60; 
     miliseconds -= (minutes * 60); 

     seconds = (int)miliseconds; 
     miliseconds -= seconds; 

     PlayerPrefs.DeleteKey("TimeOnExit"); 

    } 
    savedSeconds = 0; 
} 

public void Update() 
{ 
    // count down in seconds 
    miliseconds += Time.deltaTime; 
    if (resetTimer) 
    { 
     ResetTime(); 
    } 
    if (miliseconds >= 1.0f) 
    { 
     miliseconds -= 1.0f; 
     if ((seconds > 0) || (minutes > 0)) 
     { 
      seconds--; 
      if (seconds < 0) 
      { 
       seconds = 59; 
       minutes--; 
      } 
     } 
     else 
     { 
      resetTimer = allowTimerRestart; 
     } 
    } 

    if (seconds != savedSeconds) 
    { 
     // Show current time 
     timer.text = string.Format("{0}:{1:D2}", minutes, seconds); 
     savedSeconds = seconds; 
    } 
} 

void ResetTime() 
{ 
    minutes = defaultStartMinutes; 
    seconds = 0; 
    savedSeconds = 0; 
    miliseconds = 1.0f - Time.deltaTime; 
    resetTimer = false; 
} 

private void OnApplicationQuit() 
{ 
    int numSeconds = ((minutes * 60) + seconds); 
    if (numSeconds > 0) 
    { 
     miliseconds += numSeconds; 
     PlayerPrefs.SetFloat("TimeOnExit", miliseconds); 

     if (useElapsedTime) 
     { 
      int elapsedTicks = (int)(DateTime.Now.Ticks/10000000); 
      PlayerPrefs.SetInt("CurrentTime", elapsedTicks); 
     } 
    } 
} 
} 

답변

0

:

이 내 선수의 건강 스크립트입니다

public int curHealth; 
public int maxHealth = 3; 
Vector3 startPosition; 
public PlayerHealth playerhealthRef; 
float counter; 
public Animator anima; // drag the panel in here again 
private UI_ManagerScripts UIM; 
private PandaScript ps; 
DateTime currentDate; 
DateTime oldDate; 


void Start() 
{ 
    curHealth = maxHealth; 
    startPosition = transform.position; 
    ps = GameObject.FindGameObjectWithTag("PandaScript").GetComponent <PandaScript>(); 
    currentDate = System.DateTime.Now;  
} 


void Update() 
{ 

    if (curHealth > maxHealth) { 
     curHealth = maxHealth; 
    } 
    if (curHealth <= 0) { 

     Die(); 
    } 
} 

void Awake() 
{ 
    UIM = GameObject.Find ("UIManager").GetComponent<UI_ManagerScripts>(); 
} 

void Die(){ 

    if (PlayerPrefs.HasKey ("Highscore")) { 
     if (PlayerPrefs.GetInt ("Highscore") < ps.Score) { 
      PlayerPrefs.SetInt ("Highscore", ps.Score); 
     } 
    } else 
    { 
     PlayerPrefs.SetInt ("Highscore", ps.Score); 
    } 

    UIM.EnableBoolAnimator(anima); 
    PlayerPrefs.SetInt("RemainingLives", curHealth); 
    print("Remanining Lives: " + curHealth); 
    PlayerPrefs.Save(); 
} 

void OnApplicationQuit() 
{ 
    PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString()); 
    print("Saving this date to prefs: " + System.DateTime.Now); 
} 

void LoadSaveData() 
{ 
    PlayerPrefs.GetInt ("RemainingLives",0); 
    OnApplicationQuit(); 
} 

public void Damage(int dmg) 
{ 
    curHealth -= dmg; 
    Reset(); 
} 

void Reset() 
{ 
    transform.position = startPosition; 
    GotoMouse.target = startPosition; 

} 
} 

그리고 이것은 내 타이머 스크립트입니다 플레이어가 잃을 때까지 기다리라고 말하는 것을 보지 마라. 타이머를 시작하려면 플래그를 설정하거나 타이머 스크립트를 활성화하십시오.

플레이어 필수 입력 사항이 아닌 경우에도 PlayerPrefs를 사용하고있는 것으로 보입니다. 게임을 종료하고 재시작 한 후에도 남은 생명을 추적하고 싶지 않다면 (심지어별로 이해가 안되기도합니다.) 인스턴스 나 클래스 변수를 사용하는 것이 더 나을 것입니다.

+0

그것은 이미 내 문제를 해결했습니다 괜찮아요. 그래도 고마워. –

1

사용이 무료 매우 유용한 자산 : https://www.assetstore.unity3d.com/en/#!/content/17276

다음과 같이 뭔가를 쓰기 :

Observable.EveryUpdate() 
    .Where(_ => currentHealth <= 0) 
    .Subscribe(_ => 
    { 
     //Do something... 
     //Ex. Die(); 
    }); 

가 .Subscribe의 모든 물건을 할 때 true를 반환 곳에서 조건. Start()가 아닌 Update()에서 사용하십시오.

오 ... 당신은 몇 가지 간단한 타이머를 원하는 경우, 예를 들어 쓰기 :

Observable.Timer(TimeSpan.FromMilliseconds(1000)) 
    .Subscribe(_ => 
    { 

    }); 
+0

내 문제는 이미 해결 됐어. 그래도 감사합니다 –

관련 문제