How to rewire my source to run after deviceready in Cocoon?

92 Views Asked by At

I need help getting the game to run in the right place, inside onDeviceReady. Here is how my game is formatted as of now. I don't think the game runs well unless I change it. Mainly I'm not sure how to scope the variables and set them after deviceready. Thank you so much.

var cocoon_active = typeof(Cocoon) === 'object'
var one, two, three; // ...

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
  if (cocoon_active) {
  // ...
  }
  // FIXME CANVAS+ CODE RUNS HERE
}

window.onload = function() {
  game = new Phaser.Game(game_width, game_height, Phaser.AUTO, '');
  game.state.add("play_game", play);
  game.state.start("play_game");
}

var play = function(game){}
play.prototype = {
  preload: function() {},
  create: function() {},
  update: function() {},
  render: function() {}
} // end prototype

function a() {}

function b() {}

function c() {} // ...
1

There are 1 best solutions below

0
BdR On

Do you think the game doesn't run well, or do you actually get an error or black screen? Does the console give any error message when you try it in the CocoonJS launcher app?

The way I added some code before starting-up CocoonJS in my game was like this. I added something like the code below into a separate script and included that as source in the main index.html:

(function (cocoonjsphaser) {

    cocoonjsphaser.utils = {
        runthisfunction: function () {
            // this code will execute when Cocoon starts
        }
    };
    // etc.
}(window.cocoonjsphaser = window.cocoonjsphaser || {}));

if (navigator.isCocoonJS) {

    // run some code at startup
    cocoonjsphaser.utils.runthisfunction();
}

And I added the phaser startup code inside the <body> of the main index.html

<body>
<script>
(function() {
    // initialize the framework
    var game = new Phaser.Game(640, 960, Phaser.AUTO, '');
    // add game states
    game.state.add("play_game", play);
    // game.state.add("level_select", levelselect);
    // game.state.add("main_menu", mainmenu); //etc.
    // start a state
    game.state.start("play_game");
})();
</script>
</body>