How to stop this.scene.launch(...)?

How to stop parallel scenes ? that are called by this.scene.launch(‘scene’).

I already call this.scene.stop(‘scene’). but it’s does’t solve this problem, it’s just keep running.

Here is a live example http://cdn.redfoc.com/test/scene_test/

After you restart it, alert will be called twice and so on if restarted again.

My code

class GUI extends Phaser.Scene {
	constructor(){
		super('game_ui');
	}
	create(){
		let txt = this.add.text(20, 20, 'press "Q" to send alert\npress "A" to send yoyo\npress "R" to restart',{fontFamily: 'Arial', fontSize: '25px',color: '#fff'});

		let ourGame = this.scene.get('game');

		ourGame.events.on('alert', function (data) {
            alert(data);
        }, this);

        ourGame.events.on('yoyo', function (data) {
            this.events.emit('yoyo_receive', data);
        }, this);
	}
}
class GAME extends Phaser.Scene {
	constructor(){
		super('game');
	}
	create(){
		this.scene.launch('game_ui');
		let eventGUI = this.scene.get('game_ui');

		eventGUI.events.on('yoyo_receive', function (data) {
			console.log('yoyo received')
            alert(data);
        }, this);

		this.input.keyboard.on('keydown', function (eventName, event){
	        var key = eventName.key;
	        if(key == 'r'){
	        	console.log('restart');
	          this.scene.start('game'); //restart
	        }
	        else if(key == 'q'){
	        	console.log('send alert to GUI');
	          this.events.emit('alert', 'success!'); //send alert
	        }
	        else if(key == 'a'){
	        	console.log('send yoyo to GUI');
	          this.events.emit('yoyo', 'yoyo success!'); //send yoyo
	        }
	    }, this);
	}
}
var config = {
	type: Phaser.AUTO,
	width: 300,
	height: 400,
	scene: [GAME, GUI],
}
var game = new Phaser.Game(config);

Any help?``

UPDATE:

Fixed Event emitter , Can't communicate after scene restart/start again

Add

ourGame.events.off('alert');
ourGame.events.off('yoyo');

before ourGame.events.on(…) etc.

1 Like