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:
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)
(http://www.2dadventure.com/ags/glow.gif)
(glowing sprite while slider is being moved)
(http://www.2dadventure.com/ags/glow2.gif)
What did I do wrong?
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:
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;
}
}
}
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!
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?
By making me test my code so I can see the flaw in the logic. :P
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.
Not only does this work perfectly but it also made me lose 10 pounds!
Thanx man :)