Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Music don't toggle — Gideros Forum

Music don't toggle

GiderosFanGiderosFan Member
edited June 2015 in General questions
hello, i have following problem in my options menu i want to toggle my music on / off

the code for the main.lua part is:
-loading application settings
sets = dataSaver.loadValue("sets")
--if sets not define (first launch)
--define defaults
if(not sets) then
	sets = {}
	sets.music = true
	dataSaver.saveValue("sets", sets)
end
 
-- Loads sounds
music = {}
music.theme = Sound.new("sounds/music.mp3")
 
--turn music on
music.on = function()
	if not music.channel then
		sets.music = true
		dataSaver.saveValue("sets", sets)
	end
end
 
--turn music off
music.off = function()
	if music.channel then
		sets.music = false
		dataSaver.saveValue("sets", sets)
	end
end
 
the options menu looks like this:
 
	musicOnButton = Button.new(imgBtnMusicOn, imgBtnMusicOn, 2)
	musicOffButton = Button.new(imgBtnMusicOff, imgBtnMusicOff, 2)
 
	musicOnButton:setPosition((screenWidth-imgBtnMusicOn:getWidth())/2, imgPlaceholder:getY() + imgPlaceholder:getHeight() + 20)
	musicOffButton:setPosition((screenWidth-imgBtnMusicOn:getWidth())/2, imgPlaceholder:getY() + imgPlaceholder:getHeight() + 20)
 
 
	musicOnButton:addEventListener("click", 
		function()
			self:removeChild(musicOnButton)
					sets.music = true
		dataSaver.saveValue("sets", sets)
			music.off()
			self:addChild(musicOffButton)
		end
	)
 
	musicOffButton:addEventListener("click", function()
			self:removeChild(musicOffButton)
			sets.music = false
			dataSaver.saveValue("sets", sets)
			music.on()
			self:addChild(musicOnButton)
		end
	)
 
	if sets.music then
		self:addChild(musicOnButton)
	else
		self:addChild(musicOffButton)
	end
 
 
and in my game i called this:
 
	if sets.music then
	music.channel = music.theme:play(0,1000000)
end
so my problem is following. The music button toggle but if i get back to the main menu and get back to options there is always the music button ON. If i touch on them and get back again it's on too. The value can't changed off.

Maybe anyone can help me with an solution. Sorry I'm a newbie and learn gideros at the moment.

Comments

  • Ahhh i found the solution i have forgotten to get the sets key from music. Like this:
    if sets:get("music")
    self:addChild(musicOnButton)
    else
    self:addChild(musicOffButton)
    end
  • piepie Member
    @GiderosFan It's hard to follow unformatted code (use the 5th button ("C") above the input textfield) :)
    you can try to trace the value of sets.music with
    print("message", sets.music)
    Maybe I didn't follow correctly what you're doing, but in musicOnButton listener function I expected :
    sets.music = false
    , and in musicOffButton:
    sets.music = true


  • Thank you pie for the information yes about the formatting. i will change this :) thank you very much :)
Sign In or Register to comment.