Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs

ct.js

Create cool 2D games with a free game editor! · By Cosmo Myzrail Gorynych

How do i create a variable that switches between true and false every 3 seconds?

A topic by Tomis created 24 days ago Views: 37 Replies: 2
Viewing posts 1 to 3

As the title says I need a variable that changes from true to false every X amount of seconds, please help

Developer
  1. Set a timer variable in your OnCreate event, and the variable itself:
this.switchTimer = 60 * 3; // three seconds
this.switchVar = true;
  1. In your OnStep code, decrement the timer with ct.delta, and reset it when it is below zero, and change your variable at the same time:
this.switchTimer -= ct.delta;
if (this.switchTimer <= 0) {
    this.switchTimer = 60 * 3; // set the timer again
    this.switchVar = !this.switchVar;  // use logical NOT to change variable from true to false (and from false to true)
}
(1 edit)

Thanks so much for the detailed response! really helps me understand the concepts instead of just copying the code :).