Mute Entire App

Back to what you replied before in reply #7, how would I use ICE to make the app store/save the data? I’ve been looking at ICE stuff for a bit, so you can probably answer like I know what I’m doing. :wink: [import]uid: 39302 topic_id: 24043 reply_id: 98576[/import]

Okay, so I’ve got something set up. It’s not working, but I’m not getting an error. What am I missing or doing wrong?

local scores = ice:loadBox( "scores" )  
local player1 = 0  
  
--In the mute button function I have...  
scores:increment( "player1", 1 )  
scores:save()  
  
--In the unmute button I have...  
scores:increment( "player1", -1 )  
scores:save()  
  
--Under all the other stuff I have...  
if player1 == 1 then  
 mutebutton.isVisible = false  
 unmutebutton.isVisible = true  
else  
 mutebutton.isVisible = true  
 unmutebutton.isVisible = false  
end   

The app still mutes when I press the button, but like I explained earlier, it doesn’t keep its current state after I switch to another screen and back. [import]uid: 39302 topic_id: 24043 reply_id: 99001[/import]

Instead of incrementing “player1” you might want to set (store) the value 1 for true and 0 for false.

for true
[lua]scores:store(“player1”, 1)[/lua]
for false
[lua]scores:store(“player1”, 0)[/lua]

also, make sure you’re retrieving the stored value from ICE using retrieve so your code would look something l like this:
[lua]if scores:retrieve(“player1”) == 1 then
mutebutton.isVisible = false
unmutebutton.isVisible = true[/lua] [import]uid: 34029 topic_id: 24043 reply_id: 99019[/import]

YEAH MAN! Finally! thanks a ton!!!
I’m new, and this was a challenge. :wink: [import]uid: 39302 topic_id: 24043 reply_id: 99260[/import]

Hi
How can this be done, if I am using globals ? So, when I switch scenes, the pictures will remember if the sound is on or off?

Thank you in advance :slight_smile: [import]uid: 122802 topic_id: 24043 reply_id: 106101[/import]

Have you ever used Ice before? I would recommend checking it out if your haven’t… http://developer.anscamobile.com/code/ice
I use it for my mute buttons, and it works great. [import]uid: 39302 topic_id: 24043 reply_id: 106130[/import]

Hi Nathan

Thank you for your response. I have tried using ICE, but I think it is a little confusing. :frowning: How have you made your mute button with ICE?

Thank you in advance

[import]uid: 122802 topic_id: 24043 reply_id: 106147[/import]

I haven’t been doing this that long so this isn’t professional advice, but it works for me. :slight_smile: The stuff below is the setup I have in my app.

In your main.lua file add…

[lua]local ice = require(“ice”)[/lua]

In the file that your mute button is in add…
[lua]local scores = ice:loadBox( “scores” )
scores:save()
local muteValue = 0
–If everything is in your main.lua file, put this under the first thing I said to do.[/lua]

Then create the button functions…
[lua]local mutebuttonfunction = function (event )
if event.phase == “release” then
audio.setVolume( 0.0 ) --this mutes the sound
scores:store(“muteValue”, 1)
scores:save()
end
end

local unmutebuttonfunction = function (event )
if event.phase == “release” then
audio.setVolume( 0.3 ) --this unmutes the sound to the volume I want it
scores:store(“muteValue”, 0)
scores:save()
end
end[/lua]

Create the buttons… I use widget.newButton
[lua]local mutebutton = widget.newButton{
default = “soundon.png”,
width = 50, --I recommend making both buttons the same size
height = 66, –
onRelease = mutebuttonfunction,
}
mutebutton.isVisible = true

mutebutton.x = 25
mutebutton.y = 25

local unmutebutton = widget.newButton{
default = “soundoff.png”,
width = 50,
height = 66,
onRelease = unmutebuttonfunction,
}

unmutebutton.isVisible = false --you don’t want this visible yet

unmutebutton.x = 25 --Make sure its in the same spot as the mutebutton
unmutebutton.y = 25 --[/lua]

Here’s what I do to make them switch when they’re pressed…
[lua]local unmutebuttonappear = function(event)
if event.phase == “ended” then
mutebutton.isVisible = false
unmutebutton.isVisible = true
end
end

mutebutton:addEventListener(“touch”, unmutebuttonappear) --tells the mutebutton to “listen” for a touch, then do the unmutebuttonappear function

local mutebuttonappear = function(event)
if event.phase == “ended” then
mutebutton.isVisible = true
unmutebutton.isVisible = false
end
end

unmutebutton:addEventListener(“touch”, mutebuttonappear) --same thing…[/lua]

Finally, here’s the important step to make the app remember if it was muted or unmuted…
[lua]if scores:retrieve(“mutevalue”) == 1 then
mutebutton.isVisible = false
unmutebutton.isVisible = true
else
unmutebutton.isVisible = true
unmutebutton.isVisible = false
end[/lua]
phew… so thats what I do for a stinkin’ mute button. :slight_smile: Feel free to ask more questions!

If anyone else has anything to help me, please let me know. Thanks!

[import]uid: 39302 topic_id: 24043 reply_id: 106207[/import]

Hi Nathan

Thank you for the help and the code, much appreciated. I have almost got it to work, I just don’t know if I need to place the last code in a function or not? - In general, where have you placed the code?

This code:

[lua]if scores:retrieve(“mutevalue”) == 1 then
mutebutton.isVisible = false
unmutebutton.isVisible = true
else
unmutebutton.isVisible = true
unmutebutton.isVisible = false
end[/lua]

Thank you in advance :slight_smile:

EDIT: Nevermind, I got it to work - With globals and with ICE :slight_smile: Thank you [import]uid: 122802 topic_id: 24043 reply_id: 106284[/import]

Great! [import]uid: 39302 topic_id: 24043 reply_id: 106291[/import]

Hi Nathan

To minimize the number of functions, you can move the content of the “button appear functions”, into your mute and unmute functions. :slight_smile: Just thought that it could be usefull, to optimize your app :slight_smile:

[import]uid: 122802 topic_id: 24043 reply_id: 106337[/import]

I’ve actually tried that, but I get an error. I believe this happens because in the first function, it doesn’t know what the unmutebutton is yet. It didn’t get that far yet… :slight_smile: [import]uid: 39302 topic_id: 24043 reply_id: 106338[/import]

Hi

Ohh okay, then you need to declare your buttons on the very top of your module :slight_smile: [import]uid: 122802 topic_id: 24043 reply_id: 106376[/import]

hmmm. Won’t that produce an error also because the function that onRelease calls doesn’t exist yet? [import]uid: 39302 topic_id: 24043 reply_id: 106413[/import]

Hi again

No… this is how I have done it :slight_smile: - I am using globals instead of ICE, but the senario is the same :smiley:

[lua]local MusicOff
local MusicOn

local function UnMutePressed ( event )
if event.phase == “release” and MusicOff.isActive then

Mute = true

audio.setVolume( 1, { channel=1 } )

MusicOn.isVisible = true
MusicOff.isVisible = false

end

end

local function MutePressed ( event )
if event.phase == “release” and MusicOn.isActive then

Mute = false

audio.setVolume( 0, { channel=1 } )

MusicOn.isVisible = false
MusicOff.isVisible = true
end

end

MusicOn = ui.newButton{
defaultSrc = IMAGE_DIR…“SM2.png”,
defaultX = 30,
defaultY = 30,
onEvent = MutePressed

}

MusicOn.x = 20; MusicOn.y = 20
MusicOn.isVisible = true

MusicOff = ui.newButton{
defaultSrc = IMAGE_DIR…“SM1.png”,
defaultX = 30,
defaultY = 30,
onEvent = UnMutePressed
}
MusicOff.x = 20; MusicOff.y = 20
MusicOff.isVisible = false

if not Mute then
MusicOn.isVisible = false
MusicOff.isVisible = true

else
MusicOn.isVisible = true
MusicOff.isVisible = false
end [/lua]

I hope that helps :smiley: [import]uid: 122802 topic_id: 24043 reply_id: 106428[/import]

Sweet! Thanks! -15 lines of code for that button plus whatever I get rid of in other buttons… :slight_smile: [import]uid: 39302 topic_id: 24043 reply_id: 106434[/import]