Changing sprites with Sliders + IsButtonDown

Started by Knox, Fri 02/04/2010 02:14:13

Previous topic - Next topic

Knox

Hi,

Im just fiddling around and learning new things with scripting, and Ive come up with a (surely) simple problem. The following code "works", but there is an exception...what Im testing is with the music volume slider in the game options...basically what I want to do is as long as the left mouse button has the slider selected and the user is moving it, it changes a sprite graphic (makes "Music Volume" sprite glow)...as soon as the left mouse is released  (and the slider), that sprite gets reset back to its non-glowing sprite.

I tried this:

Code: ags

function sldMusic_OnChange(GUIControl *control)
{

  if (mouse.IsButtonDown(eMouseLeft))
  {
    if (btnMusicVol.NormalGraphic != 60)
    btnMusicVol.NormalGraphic = 60;
  }
  else if (!mouse.IsButtonDown(eMouseLeft))
  {
    if (btnMusicVol.NormalGraphic != 84)
    btnMusicVol.NormalGraphic = 84;
  }
  SetMusicMasterVolume(sldMusic.Value);
}


It works partly, only if I select/deselect the slider quickly...if I keep the left mouse clicked down for a while and slide it up and down, and then slowly release, the graphic stays in the "glow" state, even though I no longer have the slider selected.

(normal, default sprite)


(glowing sprite while slider is being moved)

What did I do wrong?
--All that is necessary for evil to triumph is for good men to do nothing.

monkey0506

The problem is you didn't let me handle the scripting! :P

Since the OnChange function is only getting called when the value of the slider is changed then it would seem to me that if the player were to change the value of the slider while still holding down the mouse (waiting until the change had been effected), but then release the mouse without changing the value of the slider again, that you would then get this problem.

In short, don't put it in the OnChange function at all:

Code: ags
function repeatedly_execute_always()
{
  if (sldMusic.OwningGUI.Visible)
  {
    GUIControl *gcat = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
    if (gcat == sldMusic)
    {
      if ((mouse.IsButtonDown(eMouseLeft)) && (btnMusicVol.NormalGraphic != 60)) btnMusicVol.NormalGraphic = 60;
      else if (btnMusicVol.NormalGraphic != 84) btnMusicVol.NormalGraphic = 84;
    }
  }
}

Knox

Haha! Dont worry, you're in charge (Im not worthy, Im not worthy...Waynes World anyone?)

Im just testin'  ;D

Ok, Ill try that and let you know what happens!
--All that is necessary for evil to triumph is for good men to do nothing.

Knox

Ok I tested that out, it works, but just one slight thing...it "flashes" (I guess every game cycle) the glow sprite on and off, instead of just leaving it to the glow state permanently while the slider is selected. How to make it so it doesnt flicker "on and off" rapidly like that?
--All that is necessary for evil to triumph is for good men to do nothing.

monkey0506

#4
By making me test my code so I can see the flaw in the logic. :P

Code: ags
function repeatedly_execute_always()
{
  if (sldMusic.OwningGUI.Visible)
  {
    GUIControl *gcat = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
    if (gcat == sldMusic)
    {
      if (mouse.IsButtonDown(eMouseLeft))
      {
        if (btnMusicVol.NormalGraphic != 60) btnMusicVol.NormalGraphic = 60;
      }
      else if (btnMusicVol.NormalGraphic != 84) btnMusicVol.NormalGraphic = 84;
    }
  }
}


Basically the else was only supposed to apply to the first part of the condition with no regard to the second part. My grouping before was linking the else to both conditions simultaneously.

Knox

Not only does this work perfectly but it also made me lose 10 pounds!

Thanx man :)
--All that is necessary for evil to triumph is for good men to do nothing.

SMF spam blocked by CleanTalk