Window.close() issue after publishing the game

I have attached follwing script to quit button in editor.But itseems not working after publishing the game.What else I can use to quit the game or close the window once user clicks on QUIT button.

var QuitGame = pc.createScript('quitGame');

QuitGame.prototype.initialize = function () {
    this.button = this.entity;
    this.button.element.on('mousedown', this.onButtonDown, this);
    this.button.element.on('mouseup', this.onButtonUp, this);
};

QuitGame.prototype.onButtonDown = function (event) {
    this.button.tween(this.button.getLocalScale()).to({ x: 1.2, y: 1.2, z: 1.2 }, 1, pc.SineOut).start();
    setTimeout(function () {
        window.close();
        
    }, 350);
};

QuitGame.prototype.onButtonUp = function (event) {
    this.button.tween(this.button.getLocalScale()).to({ x: 0.6, y: 0.6, z: 0.6 }, 1, pc.SineOut).start();
};

Hi @gouthamamin,

I think in editor works because the launch window is initiated through JS, when you click play in editor. Given it’s a JS created window, it can later get closed by JS.

Builds don’t work like that, they load from an index.html, so I’m thinking calling window.close() doesn’t make much sense hence it doesn’t work.

You could either redirect your users to a separate page e.g. your homepage when they quit the game:

window.location.href = "https://your-page.com";

Or you could have them click to play your game from a page outside your PlayCanvas build and create a window to load the build url. That way it will work similarly to how the editor works.

Hope that helps.

1 Like

homepage when they quit the game I gave this option when user clicks restart button

As Leonidas has mentioned, window.close can only be used if the window was opened by JS code

See: Window: close() method - Web APIs | MDN

2 Likes