Introduction to Time / Timer functions

5
  • 36 favourites

Index

Attached Files

The following files have been attached to this tutorial:

.capx

Stats

7,872 visits, 17,016 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Start / Stop / Restart

Now let's use triggers!

Whenever something happens (via a trigger with is pressing a button, killing a monster, whatever!), current time is saved and runs for a certain time until the next trigger activates to stop time counting.

In the Layout window, insert a button with

- the name "SSR"

- the Text property "Start/Stop/Restart" instead of "OK"

- instance variable called "checkit"

Then insert a new text with the name SSR_Timer_txt

In the Event sheet, add a global variable called "Timer_SSR".

What do we want to do:

1. When the button is clicked, the current time is saved in Timer_SSR

2. The checkit is set to 1, to tell Construct 2 that is has been pressed once

3. The text box will show a new time clock starting from 00:00 (by subtracting the reference time in Timer_SSR from the current time), only if checkit=1

4. When the button is clicked again, it'll set checkit to 0. The text box time won't continue counting.

5. If you press the button again, it'll start all over from 0 (because current time is saved in Timer_SSR!)

Later we'll see how to stop the time and continue counting the time without resetting.

So, let's add accordingly some events:

Condition: Button SSR -> On clicked

1st sub-event: Button SSR -> Compare instance variable -> checkit = 0

Actions: System -> Set Value -> Timer_SSR -> time

Button SSR -> Set value of checkit to 1

Add an else to the first sub-event and the additional condition

Button SSR -> Compare instance variable -> checkit = 1

Action: Button SSR -> Set value of checkit to 0

Good, now let's add in the Every tick-condition following sub-event:

Button SSR -> Compare instance variable -> checkit = 1

Action: SSR_Timer_txt -> Set text to zeropad(floor((time-Timer_SSR)/60% 60), 2) & ":" & zeropad(floor((time-Timer_SSR)%60), 2)

Run and test!

Click first: Timer starts counting from 0

Click again: Timer stops counting

Click a third time: Timer starts counting from 0 again

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!