Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Hareesun

I'm trying to do a few things, none of which seems to be working all too successfully for me. Any help is greatly appreciated.

1) Deduct a few seconds from a timer when a key is pressed.
- So far I have something like this, but it seems to just crash/freeze up
init: function () {
  this.levelTime = new ig.Timer();
},
update: function() {
  this.levelTimeText = this.levelTime.delta().round();

  if (ig.input.pressed('activate')) {
    this.levelTime--;
  }
}

2) Have a timer that counts down.
- I've tried what i've put below, and it almost works except rather than going from -60 to 0 I'd like it to go from +60 to 0.
this.levelTime = new ig.Timer(60);

3) Hours, minutes, seconds.
- If anyone's done this already or got the time/know-how to make a plugin for it, it'd be greatly appreciated.

Thanks again :)

1 decade ago by nefD

For Pipewalk, I just used setTimeout set to 1000ms (1 second), and in the function it would call I would increment a seconds counter and run setTimeout again. Additionally, I would assign the setTimeout to a var, so i could use clearTimeout incase I ever wanted to stop the timer.

1 decade ago by dominic

1)
// Advance the 'base' (starting time) of a timer for 5 seconds. This 
// "tricks" the timer into thinking it has been started 5 seconds later 
// than it actually was - essentially subtracting 5 seconds from the 
// time that .delta() will report
this.levelTime.base += 5;

This is undocumented and I haven't tested it, but it should work :)

2)
The .delta() method reports the time since the target time. With new ig.Timer(60); you specify a target time that is 60 seconds in the future. Thus, the delta since that future target point will begin at -60.

If you want to get the till the target time, just multiply the delta with -1:
this.levelTime = new ig.Timer(60);
var timeRemaining = this.levelTime.delta() *-1;

3)
// Ignore the millisecond part with .floor();
var time = this.levelTime.delta().floor();

var seconds = time % 60;
var minutes = ((time % 3600) / 60).floor();
var hours = (time / 3600).floor();
Page 1 of 1
« first « previous next › last »