localStorage - access a variable from index.html

  • Hi All,

    A bit difficult to explain this one...

    I want to include a script within the index.html file that looks at a variable that C2 has saved to my localStorage and then plays a music file if the variable=1. Does anyone know how to do this?

    I need to play the music file separate from the game and the runtime javascript. I hope someone can help...

    The audio file absolutely needs to play outside the c2runtime.js file.

    If the variable is called MyVar, I guess it's going to be something like:

    <script>

    if localStorage.MyVar=1

    then play musicfile.ogg

    </script>

    ...but evidently my expertise does not stretch this far - can anyone with programming skills help???

    Thanks in advance!!

  • you could try one of these.

    or if you can't I can later tonight.

    http://archive.plugins.jquery.com/plugin-tags/localstorage

  • Thanks for this, I'll check it out in detail, though am hoping to do it without any additional plugins.

  • I was able to alert a variable I set in localstorage with:

    alert(localStorage.getItem('LocalValueTest'));

    but I couldn't get it to work in Chrome (it showed null)

    also looking at the runtime I noticed there's a prefix attached to localstorage variables but I think that only applies to after they've been uploaded to the arcade.

  • Thanks for this, much appreciated - you got further than i did. Now you've given me that pointer i'll see whether i can get it to do what i need.

    Cheers!!!

  • Perhaps the plugin passC2value could help you but I wonder a little about what you want.

    Why you don't use the C2 audio object. If you write your own code to play only an oggfile, IE users wont hear anything.

    [Edit] Ok, with the call Javascript plugin this is easily possible - but i've ignored IE9 in the javascript.

    demo

    external javscript-file

    Demo-capx (requires callJs-Plugin) <img src="smileys/smiley2.gif" border="0" align="middle">

    the whole project (zip)

    [/Edit]

    [Edit2]If you really want to write a script within the file index.html then you can play with the other plugin - passC2Value. The coding should be similar. Just mention that when you export from Construct then the index.html gets overwritten. <img src="smileys/smiley2.gif" border="0" align="middle">

    [/Edit2]

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for this. The reason for doing it is phonegap to android. When the app goes to backround the music keeps playing. I've tried loads of ways to add a phonegap event listener in the index that mutes music in the javascript when backgrounding. I can't seem to switch it so am resorting to running music from the index that plays and mutes depending on the value of a variable fired from the c2 javascript file. All other sounds will stay within c2.

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