Pls help !

leminhtri

Member
I made a button to turn off and on the sound in my game, but when I press the button to turn off the music the picture changed to the speaker with no sound. But if I am turning off the music and move into another room, the music is still off but the image of the button becomes the image of a speaker with sound. If anyone knows how to help please help me! Thank you very much ! (I use google translate so please forgive me if there is any confusion)

Here is the code in button sound on - off :
- Event create:
image_speed = 0;
global.music = 1;
- Event left release:
if global.music = 1 {
audio_pause_sound(sou_background)
sprite_index = s_sou_off;
sprite = false
}

if global.music = 0 {
audio_resume_sound(sou_background)
sprite_index = s_sou_on;
sprite = true
}

global. music = !global.music
 

Attachments

Last edited:
I suggest you show your related codes for the above. I'm guessing you've a persistent object that actually manages the sound on/off which is activated/deactivated by this not persistent sound button?

If you want an action/decision made in 1 room be reflected/carry over to the next room, set as persistent. If you don't want that to happen, then don't set it as persistent.

Edit:
Note: OP added code by editing above post after my post.
 
Last edited:
Allow me to give you a simple solution, since your code above is quite confusing me, also I tried replicating the above, it doesn't work at all.

Do these;
1. Make 4 sprites (up to you) e.g.
Untitled.png
2. Name the sprite whatever you want (my example is "spr_sound_button")
3. Create an object, name it "obj_sound_button", use the above sprite for it, and set it to persistent and visible.
4. Add the sound file to use, in my example "snd_background_music"
5. code it like so;
GML:
/// 1. in the create event
// set image/sprite to
image_index = 0;
image_speed = 0;

// play this music/sound - audio_play_sound(sound, priority, loop);
audio_play_sound(snd_background_music, 1, 1);

/// 2. in the step event
if image_index == 0 {
    audio_resume_sound(snd_background_music);
} else if image_index == 2 {
    audio_pause_sound(snd_background_music);
}

/// 3. in the mouse pressed event
image_index += 1;

/// 4. in the mouse released event
image_index += 1;
6. Place "obj_sound_button" in the 1st room where you want sound to be controlled. No need to add another copy of this button. Because it's already set as persistent.

But if you don't want to set the "obj_sound_button" as persistent, in which case you have to place 1 copy of this button in every room you want, but remember the sound will play in the next room even if in the previous room it has been off-ed, in any case code it like so;
GML:
/// 1. in the create event
// set image/sprite to
image_index = 0;
image_speed = 0;
play_music = false;

/// 2. in the step event
if image_index == 0 && play_music = false {
    play_music = true; 
    // play this music/sound - audio_play_sound(sound, priority, loop);
    audio_play_sound(snd_background_music, 1, 1);
} else if image_index == 2 && play_music = true {
    play_music = false;
    audio_pause_sound(snd_background_music);
}

/// 3. in the mouse pressed event
image_index += 1;

/// 4. in the mouse released event
image_index += 1;
There are better ways to code sound on/off, but this was how I did when I first began coding in GML.

Edit:
if you want to make sure an audio file is not carried over to the next room, do this :
Untitled.png
In the room editor, look for the "creation code" button, press it, and type this "audio_stop_all();". This will make sure any sound/music played in the previous room not carry over to the next room you go to.
 
Last edited:
Top