# Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

### Newcomers AppGameKit Corner / Count from 0 to 100% in 15 seconds using timer

Message
Posted: 25th Feb 2018 20:10
Hi all

Just wondering if anyone can help or point me as I just cannot wrap my head around how to count smoothly from 0 to 100% over 15 seconds using a timer when a key is pressed. I thought about using the sleep for 150 milliseconds and then adding one a counter like below:

However would that not slow the program down while it was running?

Cheers folks
Posted: 25th Feb 2018 20:48 Edited at: 25th Feb 2018 21:11
Use framerate independent coding where a value is based on some distance per second or 60th of a second. Calculate time scale and use that as a multiplier.

For example, you seem to be looking at it as a time scale of seconds which is quite common in games to have a velocity per second and so forth. So something like this...

Will give you what you want. Just put that in the editor and run it. Press SPACE to set the keyPowerUp flag to true and the PowerPercent will increase to 100% over 15 seconds.

PowerPercent is increased by 6.667 because we are thinking in terms of seconds. You want PowerPercent to reach 100 in 15 seconds.
This means each second PowerPercent should increase by 6.667 (i.e. 100 over 15 seconds = 100 / 15 = 6.667).

However, we cannot just increment by 6.667 because obviously each loop (frame) will be some fraction of a second. This fraction of a second we have calculated and stored in FrameTimeScale.
So we just multiply 6.667 by FrameTimeScale to get the correct value PowerPercent should be increased by this frame.
If FrameTimeScale is 1/10th of a second PowerPercent will be increased by 0.6667. If FrameTimeScale is 1/60th of a second PowerPercent will be increased by 0.111116. And so on.
Point is you don't need to worry about it. This is what framerate independent code is for. It automatically sorts it out for you so you can think in terms of change over a fixed time such as 1 second.

I think I saw someone mention there is a built-in function to give you the frame time so you could just use that instead of calculating it. I am just used to calculating it myself and is super easy to do.
Also I tend to work with 1/60th of a second as my base time instead of 1 second. This is just out of habit on things like the C64 and Amiga. I always think of a frame as being 1/60th of a second even though in reality I know that is not the case today. And framerate independent code makes it so I can program everything as if every frame is 1/60th of second.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Posted: 25th Feb 2018 21:16
Here is some code with comments, let me know if something needs more clarification.

13/0
Posted: 26th Feb 2018 01:09 Edited at: 26th Feb 2018 01:09
Here's a shortened down code block that will do what you want:

My Games - Latest WIP - My Website: Immortal.Digital - FB - Twitter
130,000 installs with AppGameKit and counting
Posted: 26th Feb 2018 04:41
You could also use a tween. This sounds like overkill at first, but it means you don't have to have a linear progression.
Your power can ease in at the start, climb steadily and then slow down as it reaches maximum towards the end.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Posted: 26th Feb 2018 05:53 Edited at: 26th Feb 2018 06:04
I wrote this function for fading things. I haven't found any issues with it yet but there may be some.

Posted: 26th Feb 2018 06:07
So many ways to do anything. Lol Well at least the OP and anyone else interested have plenty of things to add to their toolbox.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Posted: 26th Feb 2018 11:07
I'd second BatVink and use a Tween, it's more or less set it and forget, you don't need to worry how fast your app runs at and it's accurate enough.

Untested:
Posted: 27th Feb 2018 00:47
Thanks very much for all the very helpful answers - I will have a go with them and see which one I can best use and understand.

Cheers
Glyn