nwjs v0.50.0 - javascript code to prevent closing window not working after export

1 favourites
  • 4 posts
From the Asset Store
Two levels with two sistem Wave. with Source-code (.c3p) + HTML5 Exported.
  • Construct 2 version: r279

    NWJS for construct version: v0.50.0

    Another issue with nwjs with Construct 2, this time I think it might be an issue with Construct rather than nwjs.

    I've been using a specific javascript/jquery code for nwjs to prevent the window from closing when pressing the 'x' on the window itself until I've run my own code. This works perfectly in preview mode inside C2, but once exported, it won't work anymore. It simply close straight away. Take example below (with minimum .capx file to test).

    I am using this code at start of layout with the browser object -> execute javascript action:

    "var win=nw.Window.get(); win.on('close', function() {c2_callFunction('quit');});"

    Then once the function called 'quit' runs, I put, for example, wait 5 seconds then I run this javascript code:

    "var win = nw.Window.get(); win.close(true);"

    Again, this example works excellent in preview mode but not in export. What do I have to do for the export version to work? I chose not to minify the export btw.

    EDIT: I should also mention I have tried setting nw.close(false) to the first snippet of code above with no difference.

    I have attached example minimum project below for testing. Remember to try it in both preview mode and exported.

    Download .capx


  • adventurist Did you manage to find a solution?

  • dop2000

    I did find a solution that worked for me, however, it's a bit of a hassle and involved many steps. In the exported version, they have added a javascript code that overrides any code you add into the editor in regards to preventing the game from closing, which is why it only works in the preview mode. Here's the steps I follow to make this work:

    1. Open the exported files folder and find package.nw

    2. Change the package.nw file-ending name to .zip instead.

    3. Extract the zip file to a new empty folder somewhere on your computer.

    4. In that folder where there now are a bunch of files from the extraction, find index.html.

    5. Open the file in an editor of choice and scroll down to this section of javascript code:

    var win = window['nwgui'].Window.get(); win.on('close', function () { window['nwgui'].App.quit(); });

    The line that you want to remove is the window['nwgui'].App.quit();

    However, in doing so, you will need to have a way inside your game to close down the game window so it's not left open after you have run your own code (such as after a "are you sure you want to close?" message). I added a call to a function inside the game as a substitute to the line removed above by using the function "c2_callFunction()".

    After that change you need to zip all the extracted files up together again, and then change the .zip file-ending to .nw again, and replace the old package.nw in the original flder with that one.

    Hope that helps.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This is awesome, thank you!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)