How do I make a stoppable timer for stamina bar ?

Get help using Construct 2

Post » Wed Nov 23, 2016 10:00 pm

Message: Tryall can only post plain text URLS until they have 500 rep. 2 URLS modified. Why?
Hello, First I am sorry for my bad english, I do my best :D

I try to make a stamina bar. If the player run, he loose his stamina. If he don't run for 3 seconds, his stamina regenerate. But I have some issues with this 3 seconds timer... Here is my code :
If Maj is pressed, Stamina over 0, and player is not stationary ---> The player run (100 is the walking speed). And he loose stamina.
And here is where i am stuck. I try to do : If the stamina is under 100 (the maximal level) and the player don't have the running speed ---> Wait 3 second before the stamina start regenerate.
But I want when the player start another action that consume stamina, the 3 second timer reset and restart at 3 second when this action is over. for finally regenerate the stamina at his maximum while the player don't re-consume his stamina.

With the actual code, If the player run while the stamina start regenerate, the stamina keep regenerating immediately after the player stop running without waiting 3 seconds.

Can you help me with this ? i'm realy stuck...
Posts: 6
Reputation: 439

Post » Wed Nov 23, 2016 11:14 pm


I think that you should look the Timer behavior

Use a timer to trigger regeneration and stop the timer when you consume stamina.

Tell us if you need help with timers :)
Posts: 1
Reputation: 227

Post » Thu Nov 24, 2016 3:50 pm

Thanks for the tips, I totaly forgot about the Timer behavior.

I successfully made the timer, from 0 to 3, If the player run again the Timer restart like I want. All is perfect, except I don't know how to start the stamina regeneration when the timer made his 3 seconds. They are no condition trigger ? How can I do ? :(
Posts: 6
Reputation: 439

Post » Sat Nov 26, 2016 3:13 am

I found ! Thank you for the timer behavior tips !

The "How to" for who need the same thing (I don't put any screenshot or project file because i'm not at home, I just have the real "Potato Internet" here.)

Create your stamina bar and a global variable named "Stamina". You can found some tutorial on the Internet for making a stamina bar who moved with your "Stamina" variable.

I set the global variable "Stamina" to 100.
My stamina bar have 4 behavior : The "Pin" behavior because my stamina bar follow the player. The "Timer" behavior for the little timing before it start regenerating. And two "Fade" behavior, one for the Fade In, the other one for the fade out (I want my bar to be invisible after 1.5 seconds if the stamina is full).

For the two "Fade" behavior, name the first one "Fade_In" with the settings : "Yes", "0.5", "0", "0", "No". And the other one named "Fade_Out" with the settings : "No", "0", "1", "1", "After fade out".

And the code :

If Keybord "Maj" is down, AND if "Stamina" over 0, AND if the speed of the player is under the walk speed. -----> Set the player speed to "run speed", subtract the stamina (0.7 for me). stop the timer (inser your timer tag, can be anything, that doesn't matter), and desactivate the "Start_Stamina_Regen" group (we gonna create it later).

If Keybord "Maj" is released -----> Set player speed to "walk speed".

If "Stamina" (global variable) is under 100, AND the speed of the player is under the "run speed" -----> Start the timer of the stamina bar for 3 seconds, "Once" with the tag "Stamina_Regen"

If "On Timer "Stamina_Regen"" condition is true ----> set group "Start_Stamina_Regen" activated.

CREATE A NEW GROUP named "Start_Stamina_Regen" (activated by default).
put in the new group : : Every 0.05 seconds, add 4 to "Stamina" (global variable).

(in no group this time) If "Stamina" (global variable) is equal or over 100 -----> Set "Stamina" to 100, set group "Start_Stamina_Regen" to deactivated, Call the function "Stamina_Bar_Fadeout" (we gonna create the function later). set group "If_StaminaUnder100" to activated (we gonna create the group later).

If "Stamina" (global variable) is under 0 ------> Set "Stamina" to 0, set the player speed to "Walk speed".

CREATE A NEW GROUP named "If_StaminaUnder100"
put in it : if "Stamina" under 100 ------> Call function "Stamina_Bar_Start" (we gonna create the function later), and set the group "If_StaminaUnder100" to deactivated.

(in no group this time) : on function "Stamina_Bar_Start" ----> Create the stamina bar were you want with your settings, you can Pin it on other objects like me if you want. and Start the "Fade_In" behavior.

On function "Stamina_Bar_FadeOut" -----> Start the "Fade_Out" behavior.

Sorry for the long post.
Posts: 6
Reputation: 439

Return to How do I....?

Who is online

Users browsing this forum: evillair, Solomon, vankizzle and 6 guests