[SOLVED] How do I create a music on/off button

Get help using Construct 2

Post » Thu Apr 10, 2014 10:50 am

hi there construct2 community!

My 1st game is nearing completion and I began to add sounds and music into it.
So far so good, but I've ran into an issue with a toggable on/off button for sounds and music

I tried the following solution:

2 Local Values (WebStorage.LocalValue("xxx"), both wit 0 (sounds/music off) and 1 (sounds/music on).
If the player clicks on the "XXX off/on" button the sound/music localValue is changed to 0 or 1, depending on what it was before.

It will look like this:
(ignore the "MusicOnOff/SoundOnOff" is visible/invisible part, this is only a button-was-pressed feedback for the player and works as intended)
Image

next step is to let music/sounds only play when LocalValue=1, this is rather easy and works.

My problem is: Both Local Values don't change on button pressed! They go from 1 -> 0, but not back to 1 if I press again. Even after switching the layout or restarting the run/debug layout the values won't change on button clicked.

I've tried different things allready, but none works :(

Help would be much appreciated!

greetings
batzu

//edit1: if I switch the order of the Local Key "SoundOn" = 0 and Local Key "SoundOn" = 1, and same for Music, it changes from 0->1 but not back.

I assume if I press the "SoundOnOff" button it runs through all of the sub-conditions, so first it will change form 0 to 1 and then immediately back from 1 to 0 or vice versa, depending on which condition comes first, because the 2nd condition immediately is true ._.
So what I need is a way to "check" what value the local key has.. I thought the way I did it worked
Last edited by genbatzu on Fri Apr 11, 2014 6:22 am, edited 2 times in total.
B
8
S
1
G
1
Posts: 29
Reputation: 735

Post » Thu Apr 10, 2014 12:37 pm

As You say it is firing that quick that it is resetting to 0, try adding a number instead...

on touched add 1 to sound on,

then another event >sound on is greater than 1 set sound on to zero...

Should work, but not tried it...
As long as I can move left, right and fire, I'm Happy...
B
42
S
15
G
11
Posts: 655
Reputation: 12,270

Post » Thu Apr 10, 2014 12:52 pm

You set the value to 1, and the next line you check to see if it's 1. Of course it's 1.

Make that an Else statement.
B
240
S
62
G
33
Posts: 903
Reputation: 40,589

Post » Thu Apr 10, 2014 12:56 pm

Event 22 and 26, add an Else (it is a system condition that will be true only if the precedent event was false during the same tick, which will work in that case, since you want to have only one of them to be true in the same tick)

Edit:ninja d
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Thu Apr 10, 2014 1:22 pm

May I ask why you're using webstorage for the control of the music/sound ?
Using R243 for all examples
B
69
S
20
G
6
Posts: 379
Reputation: 9,701

Post » Thu Apr 10, 2014 1:39 pm

Thank you all for the replies!

@Paradox & @Aphrodite

"else" is greyed out, I tried to use it, sorry I should've mentioned it, even after deleting the sub-conditions (which I think are the reason else is greyed out)

[img.]http://abload.de/img/musicsoundonoff_greyekkupm.jpg[/img.]

//"edit": creating the whole event from scratch allows me to set "else", however, the positioning seems odd to me, because it's not "linked" to the above event by the typical "line"


//edit2: oh I see where the "else" should be positioned now =)
like this?
Image
however, it's not working always, sometimes it changes to correct 0/1, sometimes it doesn't change at all, after further testing, it changes to 0/1 after 4-5 clicks on the button

@Pixel

I tried your approach and it worked =)
Image


emoaeden wrote:May I ask why you're using webstorage for the control of the music/sound ?

Sure! It's to remember the settings, so even if the player quits the game, he comes back and his settings are the same.
B
8
S
1
G
1
Posts: 29
Reputation: 735

Post » Thu Apr 10, 2014 3:54 pm

Glad to see you got it. I've used that solution before, it's the best for toggles that cycle through more than two choices.

@Aphrodite Barely a Ninja, :-) I explained why it didn't work, you explained how to fix it.

Ah, just realized why the Else was ghosted, you can't put an Else on a Trigger. But then it looks like you had figured that out too, because you had changed it to be "Is Touching" which is not a trigger. You were getting double touches because of that change. The one that worked has the trigger again, and can only be hit once per touch.
B
240
S
62
G
33
Posts: 903
Reputation: 40,589

Post » Fri Apr 11, 2014 12:55 am

I also have something similar but i dont know why is only working the first time.

Image

i know i should do it like genbatzu with webstorage but why it only change only the fist time?
B
31
S
13
G
6
Posts: 355
Reputation: 5,058

Post » Fri Apr 11, 2014 6:20 am

@katzin I think it's the same issue I had. You use the same button for both actions (volume on/off). On the first touched command the "VolumeVariable" goes from 0 to 1, as expected, but then it automatically triggers the 2 command, because VolumeVariable=1 is now true, and the action for this condition is to set VolumeVariable from 1 to 0



Image

this here is similar to "Pixel perfick"s solution and works, but it's not perfect
B
8
S
1
G
1
Posts: 29
Reputation: 735

Post » Fri Apr 11, 2014 1:20 pm

But the thing is that also the frame it only works once.. :S, changed to the other frame but then it not goes back when i touch it again.
B
31
S
13
G
6
Posts: 355
Reputation: 5,058

Next

Return to How do I....?

Who is online

Users browsing this forum: tarek2 and 24 guests