for (int i = 0; i < MaxValue+1; i += 10)
So I am working on an app that displays numbers [Talk about being vague haha] and suddenly just an hour or so ago I thought, hmm why not instead of just plain numbers being shown instantly, display it with that classic game thing where the values hop in increments into the value to be shown… which gives a pleasing sensation while the values are shown instead of just showing a static value…
So as I am doing this in UWP I had to use Task.Delay to delay the loop but on desktop you would use Thread.Sleep etc… there are other ways to do this too…
Basically inside the for loop you would perform the display and other things and then add the sleep call
For me it was this call:
await Task.Delay(TimeSpan.FromMilliseconds(1));
I tried ticks but I had to use 10000 ticks and well it was the same as 1ms… anything less and it just showed the max value instantly lol [To be honest I am not used to Ticks as something to be used so any info would be nice]
So in whole:
MaxValue = 2500; // Set the MaxValue value
TotalHealthPoints = 0; // starting value
async private void SomeTask() // Method Call
{ // Start the method
for (int i = 0; i < MaxValue+1; i += 10) // Set up the FOR LOOP
{ // Start the loop
TotalHealthPoints + i // we increment this until the total value is reached on each loop
// Display Update Code Here
await Task.Delay(TimeSpan.FromMilliseconds(1)); // delay the increment by as much as you like
} // End the loop
} // Close the method
// if you have say a float value with something after the decimal point then
// you can set the displayed value after the increments has occurred as a final
// show which gives a nice finish
In the += 10 place, you can decide the hop or jumps in increments so if your max value is expected to be 10,000 I would hop by 125 or more depending on how subtle you want the appearance to be… alternatively you could precede the for loop with some checks to see where the MaxValue sits, so you can have an int check and instead of += 10 you would have your int say IntCheck so ‘+= IntCheck’ where the value of IntCheck is defined by the checks performed on MaxValue
Like this:
if(MaxValue < 500)
{ IntCheck = 10}
if(MaxValue > 500 && MaxValue < 1000)
{ IntCheck = 20}
if(MaxValue > 1000 && MaxValue < 2000)
{ IntCheck = 30}
...
async private void SomeTask() // Method Call
{ // Start the method
for (int i = 0; i < MaxValue+1; i += IntCheck) // Start the FOR LOOP
... // etc.
You get the idea, and I am sure there are more optimised or easier to read [or not] algorithms, if you can share them here that would be nice too…
Just thought this was worth sharing on here as in a game where the user has numeric values to look at, a pleasing effect is when values increase in a sort of animated way… like when the game level begins or loads and you have say a Money value on display, usually above 1,000 so you could animate this as soon as it is displayed instead of just displaying it…
Keep in mind you could also make this work for addition and subtraction effects too…
I think the idea would be to set the starting i as the starting or current value:
// Addition:
for (int i = CurrentValue; i < CurrentValue+AddedAmount; i += 1)
// Subtraction:
for (int i = CurrentValue; i > CurrentValue-SubtractedAmount; i -= 1)
If that can be improved I would be grateful…
I was thinking to start a useful code thread, what do you think?
Mind you, this is a non-blocking method so it leaves the application free to do other things…