Solution: Run game local (file:///)

Discuss game development design and post your game ideas

Post » Sat May 07, 2016 10:59 pm

I've recentelly had to build a mobile app that could download games build with Construct 2 and run locally, the solution I've got is handle the game data and sounds in a different way. And here is my solution:

1- Export your game with the minify option unchecked
2- Change the way Construct handle sounds, to do that we need to open the index.html and add right after <body> the code:
Code: Select all
   <div style="display:none;">
   <script>
   window.playAudioAux = function(url){
      var output = url.substr(0, url.lastIndexOf('.')) || url;
      var url1 = output+'.ogg';
      var url2 = output+'.mp3';
      var url3 = output+'.m4a';
      document.getElementById('myAudioogg').src = url1;
      document.getElementById('myAudiompeg').src = url2;
      document.getElementById('myAudiomp4').src = url3;
      document.getElementById('myAudio').src = url3;
      document.getElementById('myAudio').load();
      document.getElementById('myAudio').play();
   }
   </script>
   <audio id="myAudio" controls>
     <source id="myAudioogg" src="" type="audio/ogg">
     <source id="myAudiompeg" src="" type="audio/mpeg">
     <source id="myAudiomp4" src="" type="audio/mp4">
      Your browser does not support the audio element.
   </audio>
   </div>


This will create a new way to run audio. And now we have to change the c2runtime.js where it calls the sounds, so find:

Code: Select all
   function C2AudioInstance(buffer_, tag_)
   {


And add right after that

Code: Select all
      playAudioAux(buffer_.src); return;


This will stop the normal call of Construct and call the function that we just added on the index.html

3- Most(maybe all) browsers see requests from local as a security problem so we have to load that game data.js in a different way, open it so you can copy its content. Also inside c2runtime.js find the following code inside requestProjectData function:

Code: Select all
      xhr.open("GET", datajs_filename, true);
      var supportsJsonResponse = false;


And then add after that this code:

Code: Select all
self.loadProject(FULL_CONTENT_INSIDE_YOUR_DATA.JS); return;


This will load your game content and cancel the request to load data.js.

4- Inside index.html comment the alert about running the game locally like this:

Code: Select all
//alert("Exported games won't work until you upload them. (When running on the file:/// protocol, browsers block many features from working for security reasons.)");


That is it! :D, It runs fine inside firefox, android webview etc. The only one that still doest run it is Chrome for security reasons...

Hope it helps anyone with this kind of problem.
Yours,
Diogo Araujo
B
4
Posts: 1
Reputation: 202

Post » Sat Nov 05, 2016 6:41 pm

Good morning. I did not understand where to put the code to run in local mode. Run game local (file: ///), Thanks
B
3
Posts: 1
Reputation: 177


Return to Game Development, Design & Ideas

Who is online

Users browsing this forum: No registered users and 0 guests