How do I Create A Random Timer?

0 favourites
  • 6 posts
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • What I'm looking to do is have a timer that counts down to zero every time a level begins, but I want the starting number to vary each time the level is played. Is there an example of this somewhere?

  • Here you go.

    Create a global variable "Random Timer"

    Create 3 separate Events

    Condition: On Layout start

    Action: Set variable Random Timer to "round(random(a,b))" where A is your min number and B is your max number. This will select a number between the two.

    Condition: Every 1 Second

    Action: Subtract 1 from Random Timer

    Condition: Variable Random Timer <= 0

    Action: Do what you want here..

    Edit: Added Round Expression. Thank you for pointing that out RamPackWobble

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Badmiracle - If you change your last event from Variable Random Timer = 0 to Variable Random Timer <= 0 then it should work .

    As you have it the timer will be a floating number and might never = 0 (it might be 0.4 then -0.6 for example)

    or you could round the random to the nearest whole number.

    There are other ways to do this.

  • no problem.

    here is another method.

    https://goo.gl/5z52Ix

  • Badmiracle

    Instead of

    Condition: Every 1 Second

    Action: Subtract 1 from Random Timer

    Use

    Condition: Every tick

    Action: Subtract dt from Random Timer

    to be more accurate.

  • For the random number it is better to use "floor(random(b+1))" (or "choose" function if you don't have many numbers to choose from) instead of "round(random(a,b))".

    round(random(a,b)) does not generate numbers equally, border numbers (a and b) have 50% less chances to be generated than the numbers between "a" and "b".

    EDIT: it should actually be "floor(random(b-a+1)) + a" if you want a range between f.ex. 5 and 10. What I've mentioned before is for ranges starting from 0, like 0 to 10.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)