On/Off button for music [SOLVED]

Started by Mazoliin, Sat 13/10/2007 18:41:22

Previous topic - Next topic

Mazoliin

I have been struggeling with this for a while, searched the forum and everything, and now I'm out of ideas.
I'm trying to have an ordinary options gui with game speed, music volume and if the music is on or off.

Game speed and music volume are sliders and works. The music On/Off is a button and it works to. The problem is when I change room. If I change room with the music off, and then turn it on, the right music are being played (no problems here), but if I change room with the music on, no music are being played until I press the 'Music On/Off" button.
It only works to change room with the music on if I haven't touched the 'Music On/Off' button.

Any ideas? I guess it's something with the on_event script. Only problem is I don't know what!

Code: ags

----------------------------------
Music On/Off button
----------------------------------
#sectionstart btnMusicOff_Click  // DO NOT EDIT OR REMOVE THIS LINE
function btnMusicOff_Click(GUIControl *control, MouseButton button) {
  Musik = Room.MusicOnLoad;
  Buffer = "Off";
  if(IsMusicPlaying() == 0){ //Starts music (ON)
    Buffer = "On";
    PlayMusic(Musik);
    musiconoff = 1;
  }
  else StopMusic(); lblMusicOnOff.Text = Buffer; musiconoff = 0; //Stop music (OFF)
}
#sectionend btnMusicOff_Click  // DO NOT EDIT OR REMOVE THIS LINE

----------------------------------
on_event
----------------------------------
#sectionstart on_event  // DO NOT EDIT OR REMOVE THIS LINE
function on_event(EventType event, int data) {
  if (event == eEventEnterRoomBeforeFadein){
    if (musiconoff == 0){ //Music Off
      StopMusic();
    }
  }
}
#sectionend on_event  // DO NOT EDIT OR REMOVE THIS LINE

Ashen

#1
It looks like a problem with braces. Change the else condition to:
Code: ags

else {
  StopMusic(); 
  musiconoff = 0; //Stop music (OFF)
}
lblMusicOnOff.Text = Buffer; // I think you want this to run either way? If not, move it up a line, to before the '}'


As it is, only the first line is attached to the else, the others will run everytime the button is pressed. So, musiconoff is always 0, and the on_event condition stops the music, even when you want it to be playing.
I know what you're thinking ... Don't think that.

Mazoliin

wow... I feel like a fool right now.  :-[
I don't know from where, but I have got the idea that you can do else statement like that. Well, you learn as long you live.
But you can do like this:

if (blah blah){
  //code
}
else blah

Right?

Btw, it works fine now :)

frission

Quote from: Monkey Entertainment on Sat 13/10/2007 19:39:32
wow... I feel like a fool right now.  :-[
I don't know from where, but I have got the idea that you can do else statement like that. Well, you learn as long you live.
But you can do like this:

if (blah blah){
  //code
}
else blah

Right?

Btw, it works fine now :)

Right. But if you don't use braces after an else (or an if), it will only process the next command as being part of that conditional statement. So yes to blah;, no to blah; blah; blah;.

SMF spam blocked by CleanTalk