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

  • 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:

    	<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>[/code:2dnaw682]
    
    This will create a new way to run audio. And now we have to change the [b]c2runtime.js[/b] where it calls the sounds, so find:
    
    [code:2dnaw682]	function C2AudioInstance(buffer_, tag_)
    	{[/code:2dnaw682]
    
    And add right after that 
    
    [code:2dnaw682]		playAudioAux(buffer_.src); return;[/code:2dnaw682]
    
    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 [b]data.js[/b] in a different way, open it so you can copy its content. Also inside [b]c2runtime.js[/b] find the following code inside requestProjectData function:
    
    [code:2dnaw682]		xhr.open("GET", datajs_filename, true);
    		var supportsJsonResponse = false;[/code:2dnaw682]
    
    And then add after that this code:
    
    [code:2dnaw682]self.loadProject(FULL_CONTENT_INSIDE_YOUR_DATA.JS); return;[/code:2dnaw682]
    
    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:2dnaw682]//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.)");[/code:2dnaw682]
    
    That is it!  , 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
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Good morning. I did not understand where to put the code to run in local mode. Run game local (file: ///), Thanks

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