How to terminate the module runned with require.run() in Windows Phone 8.1 app?

21 views
Skip to first unread message

splash27

unread,
Jan 4, 2015, 12:59:06 PM1/4/15
to gam...@googlegroups.com

I'm working on a game for WP8.1. gameJS library uses some kind of Yabble.js. The html page with the game canvas contains the following code:

WinJS.UI.Pages.define("/pages/game/game.html", {

    ready: function (element, options) {
        require.setModuleRoot('/js/');
        require.run('main');            
    }
});

The main.js file contains the main game loop and all other game stuff. So, when I first navigate to game.html, then everything works fine. But if I go away from game.html and then go to this page again (for example using back button) I can see only white screen.

I can see in debugger that the main.js runs again and seems like it causes this trouble. Two game loops just collide each other. How to completely terminate and unload main.js when user goes away from game.html? Or how to check is there a main.js module already runned to prevent it from second running?

Simon Oberhammer

unread,
Jan 6, 2015, 4:10:47 AM1/6/15
to gam...@googlegroups.com
Hi splash27,
I can't test on wp but two quick thoughts:

Are you sure winjs' ready function isn't the one being called twice? I can't imagine how gamejs/yabble do not terminate after the page is left. And you don't have to use yabble if you have another way to require gamejs's commonjs modules in wp/winjs. I would assume others are trying to import commonjs/amd/nodejs modules and it's probably easier to find a different way to require the modules and not run yabble inside winjs (if that's what's happening).

Hth
 Simon

splash27

unread,
Jan 6, 2015, 11:33:55 PM1/6/15
to gam...@googlegroups.com
Hi, Simon!

Thanks for answer!
WinJS' ready function fires every time, when I go to the page. But when I walk away from the page WP doesn't terminate this page completely, it keeps the page in navigation history with all scripts running. That's why when I go to the page again, then ready function fires again and runs new instance of "main.js". This new instance starts to conflict with first one. I'm not sure that WinJS has integrated way to run commonjs modules asynchronously. I'll try to search the info about it, but I think It will not solve the problem.

вторник, 6 января 2015 г., 14:10:47 UTC+5 пользователь Simon Oberhammer написал:

si...@nekapuzer.at

unread,
Jan 7, 2015, 3:16:00 PM1/7/15
to gam...@googlegroups.com
Hi splash,

> WinJS' ready function fires every time, when I go to the page.

Is there a way to detect whether the script is "waking up" (e.g. from navigating back to it) or being freshly started? That could help.

> I'm not sure that WinJS has integrated way to run commonjs modules asynchronously. I'll try to search the info about it, but I think It will not solve the problem.

It seems to me winjs is using AMD modules? or at least it's possible to use requirejs with winjs afaict from http://blogs.msdn.com/b/dave_crooks_dev_blog/archive/2014/01/17/requirejs-windows-8-winjs-and-cross-platform.aspx

you can convert GameJs commonjs into AMD like so: http://requirejs.org/docs/commonjs.html#autoconversion maybe that's easier.

simon

> --
> You received this message because you are subscribed to the Google Groups "GameJs" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to gamejs+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

splash27

unread,
Jan 10, 2015, 4:56:00 AM1/10/15
to gam...@googlegroups.com
Hi, Simon!
My temporary solution is 
WinJS.UI.Pages.define("/pages/game/game.html", {
        ready: function (element, options) {
            require.setModuleRoot('/js/');
            require.run('main');

            WinJS.Application.onbackclick = function (evt) {
                WinJS.Application.stop();
            }
        }
    });

So, when I enter the page first time it runs my main.js file with game logic. When I press back button, WinJS.Application.stop() disables all events' handlers at every page. So if I go to this page again, the ready event will not be triggered and the main.js will not be runned again.
This solution is bad, because If I have more than 1 page, stop() function will disable the events at every page.

Other thoughts: It's possible to place in LocalStorage a list of runned modules and check it every time when I'm going to run the module. But this way still contains a lot of constraints, because it is necessary to clear this list before exiting the app. And what if the game just crashed last time? This list will not be updated. Also WP apps have a suspend state. This also requires some workaround.

I can't find the reason to change GameJS Yabble to require.js or other loaders, because there is no loader that can give an info about asynchronously runned modules. Anyway other loaders also will launch main.js again and cause conflicts.

One more question. How to pause or break the game loop in GameJS which is defined like this?
gamejs.onTick(function (msDuration) {
     //game loop logic
 
});

It doesn't matter for my current game, because it is just a puzzle and it can wait for user forever, but other games might have time events and it is necessary to pause or stop them, when user has left the page.

Thank you!

четверг, 8 января 2015 г., 1:16:00 UTC+5 пользователь Simon Oberhammer написал:
Reply all
Reply to author
Forward
0 new messages