localStorage - access a variable from index.html

Get help using Construct 2

Post » Sat Mar 24, 2012 5:05 pm

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!!
B
31
S
11
G
7
Posts: 197
Reputation: 6,682

Post » Sat Mar 24, 2012 6:41 pm

you could try one of these.
or if you can't I can later tonight.

http://archive.plugins.jquery.com/plugin-tags/localstoragetwdead2012-03-24 18:41:51
B
10
S
4
G
1
Posts: 37
Reputation: 1,481

Post » Sat Mar 24, 2012 6:55 pm

Thanks for this, I'll check it out in detail, though am hoping to do it without any additional plugins.
B
31
S
11
G
7
Posts: 197
Reputation: 6,682

Post » Sun Mar 25, 2012 6:00 am

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.
B
10
S
4
G
1
Posts: 37
Reputation: 1,481

Post » Sun Mar 25, 2012 9:51 am

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!!!
B
31
S
11
G
7
Posts: 197
Reputation: 6,682

Post » Sun Mar 25, 2012 10:30 am

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)
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.
[/Edit2] Joe72012-03-25 11:35:24
B
14
S
6
G
5
Posts: 173
Reputation: 4,742

Post » Sun Mar 25, 2012 12:24 pm

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.
B
31
S
11
G
7
Posts: 197
Reputation: 6,682


Return to How do I....?

Who is online

Users browsing this forum: mercuryus, Yahoo [Bot] and 13 guests