Timer in P3

I’m porting a project from P2 to P3.
In P2, the code triggered a timed event to happen as follows:

tick: function(){
var count;
var snd = new Audio(“mysound.mp3”);
snd.play();
// time between events is needed and follows exponential distribution
var next = - Math.log(1 - Math.random()) / this.lambda;
//loop the sound to re-trigger
game.time.events.add(next* 1000, function(){this.tick(count+1);}, this);
},

I’m having trouble with the event timer in P3. I keep getting an errorr, “cannot read property ‘time’ of undefined” for let timer = this.time.addEvent({…});

advice on converting my code please? thank you!

It’s telling you

let timer = (undefined).time.addEvent({…});

I’m still hacking away at a timer. I want the timer’s time delay to update but I can’t seem to get it to work. It stays with the initial settting.

create ()
{
let sond = this.sound.add(‘sond’);
let t = this;

    this.lambda = 1;
    this.next = 1;
    
    let timedEvent = this.time.addEvent({
         delay: this.next * 1000,
         callback:  tick,
         callbackScope: this,
         loop: true

    });

     function tick(){
       t.next = -Math.log(1- Math.random() / t.lambda);
       sond.play(),           
    }    
          
}

update(){

    this.input.on('pointermove', (pointer) => {
             if(pointer.x < 400){
                 this.lambda = 4;
             }
             else if(pointer.x > 400){
                 this.lambda = 1;
             }
    });

}

You need to modify the timer event somewhere.

timedEvent.delay = newDelay;

Sorry, I’m confused. Where do I put that? newDelay is a function?

function tick() {
  t.next = -Math.log(1 - Math.random() / t.lambda);
  timedEvent.delay = 1000 * t.next;
  sond.play();
}
1 Like

Thank you i was stuck on that one for a while!