0 Favourites

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

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

    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

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

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

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • 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

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

  • 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) 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?

    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 =)

    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.

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

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

    i know i should do it like genbatzu with webstorage but why it only change only the fist time?

  • 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

    this here is similar to "Pixel perfick"s solution and works, but it's not perfect

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

  • katzin genbatzu

    You can also check which animation frame is set instead of using the variable. If it's set to frame 2, the next line switches it back to 0.

    Just make sure the animation speed is set to 0, or it will always change itself to the last frame.

    Actually here is the shortest method possible to toggle frames:

    However Sound can't be set that way, so it doesn't help much in this case.

    edit: If you don't know what that's doing, the formula in the parentheses is either true or false, 1 or 0, so 1 times the answer to our question.

    1 times 0 sets it to 0, 1 times 1 sets it to 1.

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