Hello Guest

Author Topic: Time Bar in 2D Toolkit  (Read 6039 times)

mradebe_eti

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 26
    • View Profile
Time Bar in 2D Toolkit
« on: August 20, 2013, 07:30:40 pm »
Hello,

I was wondering how I would go about making a decreasing time bar with 2d toolkit? I have already created a background and foreground image for the time bar.

Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Time Bar in 2D Toolkit
« Reply #1 on: August 20, 2013, 07:47:55 pm »
I suggest looking at the tk2dUIProgressBar. There is an example in the UI demo scene.

mradebe_eti

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Time Bar in 2D Toolkit
« Reply #2 on: August 21, 2013, 01:22:45 am »
Thanks that is very helpful.  :D

mradebe_eti

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Time Bar in 2D Toolkit
« Reply #3 on: August 29, 2013, 08:00:52 pm »
Hi there,

I'm still experiencing issues with setting up a time bar with the UI Demo scene. I have an existing timer that works and I've copied the progress bar into my scene as well as the a portion of UIDemoController script into my timer script. The progress bar doesn't seem to be work, is there an issue with my script or do I need to attach it to a different Game Object?

Here is my timer script:

                public static float curSeconds = 59;
      public static float curMinutes = 4;
      public static float startSeconds = 0;
      public static float startMinutes = 0;
      public static float startTime;
      public static int finishTime;
      public static float currentTime;
      public static float roundedRestSeconds;
      public static float displaySeconds;
      public static float displayMinutes;
      public static float countDownSeconds;
      public static float restSeconds;
      
           public tk2dUIProgressBar progressBar; //UI Demo code
               private const float TIME_TO_COMPLETE_PROGRESS_BAR = 2f; //UI Demo code
               private float progressBarChaseVelocity = 0.0f; //UI Demo code
 
   void Start () {
      GameObject.Find ("TimerText").guiText.text = "Time Remaining: " + curMinutes.ToString("f0") + ":0" + curSeconds.ToString("f0");
       guiText.material.color = Color.black;
       startTime = Time.time;
      finishTime = (int)(Time.time);
      
   }

   
   void OnLevelWasLoaded(int level) {
      if (level == 8)   {
         GameObject.Find ("TimeSpentText").guiText.text = " " + finishTime.ToString();
             guiText.material.color = Color.black;
      }
      
   }
   
   
 
   void Update () {
       currentTime = Time.time - startTime;
       restSeconds = countDownSeconds - (currentTime);
 
       roundedRestSeconds = Mathf.CeilToInt(restSeconds);
       displaySeconds = roundedRestSeconds % 60;
       displayMinutes = roundedRestSeconds / 60;
 
       string text = string.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
      GameObject.Find ("TimerText").guiText.text = " " + text.ToString();
       guiText.material.color = Color.black;

       startTime = startMinutes + startSeconds;
       startTime = Time.time;
       if(curSeconds <= 0) {
          curSeconds = 59;
       if(curMinutes >= 1)
         {
         curMinutes--;
         }
       else {
         curMinutes = 0;
         curSeconds = 0;
            Application.LoadLevel("S1P1"); // If time runs out, the player will need to restart the level
            Score.score = 0; //It will also reset the score
            curMinutes = 4;  //And reset the time
               curSeconds = 59;
         // This makes the guiText show the time as X:XX. ToString.("f0) formats it so there is no decimal place.
         GameObject.Find ("TimerText").guiText.text = curMinutes.ToString("f0") + ":0" + curSeconds.ToString("f0");
       }
    }
    else {
       curSeconds -= Time.deltaTime;
    }
    //These lines will make sure the time is shown as X:XX and not X:XX.XXXXXX
    if(Mathf.Round(curSeconds) <= 9) {
       GameObject.Find ("TimerText").guiText.text = curMinutes.ToString("f0") + ":0" + curSeconds.ToString("f0");
    }
    else {
       GameObject.Find ("TimerText").guiText.text = curMinutes.ToString("f0") + ":" + curSeconds.ToString("f0");
    }
   }
   
      private IEnumerator MoveProgressBar() //Altered UI Demo code
    {
        while (progressBar.Value < 2f)
        {
            progressBar.Value = startTime/TIME_TO_COMPLETE_PROGRESS_BAR;
            yield return null;
            startTime += tk2dUITime.deltaTime;
        }

        while (progressBar.Value < 0.0f)
        {
            float smoothTime = 0.5f;
            progressBar.Value = Mathf.SmoothDamp( progressBar.Value, progressBarChaseVelocity, ref smoothTime, Mathf.Infinity, tk2dUITime.deltaTime );

            yield return 0;
        }
    }
}

I'm still learning how to script, so please let me know where I am going wrong?

Thanks.

gary-unikronsoftware

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 74
    • View Profile
Re: Time Bar in 2D Toolkit
« Reply #4 on: August 30, 2013, 09:52:42 am »
Hi, 

The good news is I managed to get your code sort of working.  I haven't tried the entire code - just concentrating on the MoveProgressBar function...

1)  progressBar.Value is clamped between 0 and 1 as it represents a percentage.   So your line progressBar.Value < 2f will ALWAYS be true and the function will not continue past that while loop.

2)  Are you actually calling MoveProgressBar from anywhere?  I can't see it being called in the code...  Bear in mind it is a Coroutine and will need to be called with StartCoroutine(MoveProgressBar())

3)  After the initialisation of startTime in the Start function, in the Update you assign to it twice in a row: startTime = startMinutes + startSeconds and startTime = Time.time.  Time.time will overwrite the startMinutes + startSeconds value.  Did you mean to do this?

4)  You are also updating startTime in MoveProgressBar.  As this is a coroutine it will be called every frame whilst it is active, and the Update function is called every frame too.  So startTime will be set in Update, then in MoveProgressBar, then in Update, then in MoveProgressBar and so on.  I don't think this is what you want to happen.

I hope this is sufficient to get you on your way to fixing your code.

mradebe_eti

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Time Bar in 2D Toolkit
« Reply #5 on: August 31, 2013, 02:07:09 am »
I really appreciate this. This clears up a lot of the issues.

I didn't mean to assign the startTime multiple times, so good catch there.

I should be fine.

Thanks!