Hi, I was able to resolve my issue, and thought I would edit this post to help others. It wandered a bit from the original topic, so I will delete some answers and post my results so we can mark as resolved.
My original question was concerning the proper way to build an audio toggle for a game. I had been stuck due to a combination of mistakes in my JavaScript and limited knowledge of what this framework allows. That limitation is not helped by the occasional missing or incorrect documentation here. So anyone looking to do the same…
First I created a toggle button to switch audio on and off.
//Create each Audio button
btnAudioOff = cc.MenuItemImage.create(s_audio_off_btn);
btnAudioOn = cc.MenuItemImage.create(s_audio_on_btn);
btnAudioOn.setAnchorPoint(cc.p(0.5, 0.5));
btnAudioOff.setAnchorPoint(cc.p(0.5, 0.5));
//Create Audio Toggle, add the buttons to it
audioToggler = new cc.MenuItemToggle.create(btnAudioOn);
audioToggler.addSubItem(btnAudioOff);
//this sets the target function of the toggle button
audioToggler.setTarget(this.toggleAudioFunction, this);
audioToggler.setPosition(cc.p((size.width)-109,36));
There are some other ways to do this, but the documentation for cc.MenuItemToggle is limited and has some inaccuracies. This works.
The issue is that in each scene I had to display the toggle again, after removing it from the previous scene. So when I switched scenes, the current state was lost. For this I created a boolean myGameAudio. When the new scene loads, I check if this is true or false. I had set this value in the targeted function mentioned above.
So that is straightforward, but the final step took a little time. When the new scene loads, we have the value, but we need to address the toggle we created, as when it initializes it will go to its’ default state. I needed a way to match it to the value of myGameAudio at init. This is done by addressing the array of images in the toggle. Remember it starts at zero, so:
if (myGameAudio===true) {
console.log("start with Audio On");
audioToggler.setSelectedIndex(0); //set icon to on
}
if (myGameAudio===false) {
console.log("start with Audio Off");
audioToggler.setSelectedIndex(1); //set icon to off
}
cc.MenuItemToggle is amazing, but it has complexities not documented, and I believe there is even an error in the 2.1.4 API documentation. I spent a bunch of hours on this, so hopefully it will help someone.
Thanks