• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML on/off button

JJampion

Member
hi has anyone an idea how it works that a button switch from on to off
like when the button is clicked : new sprite + deactivated music
and if its clicked again : old sprite + music is activated again
 

flyinian

Member
You can do something like this.

GML:
// Create

_Active = false;

_Sprite = "Your_desired_sprite";



// Step

if(position_meeting(mouse_x, mouse_y,"Button_to_be_clicked_to_activate") && mouse_check_button_pressed(mb_left))
{
    if(!_Active)
    { // If false
        _Active = true;
    }
    else
    {// If true
        _Active = false;
    };
};



If(_Active )
{// What to when its active
// Code here
// Music active
// Sprite set
}
else
{// What to do when it's not active
// Code here
// Music deactivate
// Sprite set
};
 
Last edited:
what you can do is write something like this:

GML:
clickleft = mouse_check_button_pressed(mb_left);
somekindofboolean = false

if clickleft and somekindofboolean {
    
    somekindofboolean = false
}
else if clickleft and !somekindofboolean {
    somekindofboolean = true
}

if somekindofboolean {
    //do whatever you want when the switch is on
}
if !somekindofboolean {
    //do whatever you want when the switch is off
}


the somekindofboolean variable acts as the state of the switch, so when the switch is on, somekindofboolean is true, and if the switch is off, the variable is false. then in the 2 if statements, you can add what you want to happen. is that what you meant? :)
 
P

ParodyKnaveBob

Guest
Howdy, JJampion, and welcome to the GMC. $:^ ]

I know this has essentially been answered already, but I wanted to also answer for two reasons.
  1. I've been away for over a year because reasons. I could really use the quickie practice. $:^ P
  2. I find the above answers could be trimmed down a lot.
Now, there's plenty that could be said about object parenting and bit-flipping a bunch of different settings (and whether or not your sprite is animated and thus really needs two sprites or just one sprite with two frames, one on, one off), but right now, let's just stick to what you have here.

Let's say that your sprites are spr_btn_music_on and spr_btn_music_off with simple sprite masks, and that your default stuff is on. Let's also say you have a good way already to start your music, pause/mute/end your music, and resume/unmute/restart your music, whether via a main controller or this button or a global script or whatever. ~shrug~ You fiddle to what you need, obviously!

~~~~~~~~~~~~~​

Music button, Create Event: (the moment the game creates this instance in the room)
GML:
is_music = true;
// music playing code
Music button, Left Mouse Button Pressed Event: (the moment the player clicks the instance with the left mouse button)
GML:
is_music = !is_music;

if (is_music) {
    sprite_index = spr_btn_music_on;
    // music on code
} else {
    sprite_index = spr_btn_music_off;
    // music off code
}
That's it! I hope this helps,
 
Top