Call to timer methods not working

Hello, in my game, I have a timer that is constantly looping that I want to turn off for a bit and then turn on again later. I declare the timer initially like this:

var enemyTimer = this.time.addEvent({
	delay: 1500,
	callback: this.changeEnemyDirection,
	callbackScope: this,
	loop: true
});

which works fine. However, I have not been successfully able to remove and recreate it. I’ve been trying to use the Timer API’s remove() method like this:

this.enemyTimer = this.time.remove({
		delay: 1500,
		callback: this.changeEnemyDirection,
		callbackScope: this,
		loop: true
});

Is there something wrong with my syntax? I couldn’t find anything online yet that shows it otherwise.

Try call just this.enemyTimer.remove();

The same issue comes up where it says “Cannot read property ‘remove’ of undefined” when I tried that.

You declared:

var enemyTimer.

But call it with this.enemyTimer.

Try to declare with this.enemyTimer and it will work

This was what I tried when I got the “Cannot read property of ‘remove’ of undefined” :((remove()

In your first screen of this post, replace var by this
var enemyTimer is a variable
this.enemyTimer is an attribut
There are not the same, i suggest you to read tutorials about class in js, you’ll learn things useful to develop with phaser3

2 Likes

Like @BlunT76 said, change:

To:

this.enemyTimer = this.time.addEvent({
	delay: 1500,
	callback: this.changeEnemyDirection,
	callbackScope: this,
	loop: true
});
1 Like

Thanks guys!