Page 1 of 1

Slider's script

PostPosted: Sat Feb 02, 2008 2:25 pm
by Thanx
Hi everyone!
Lately I've started making a game, where you can have a number of players from 2-8. To keep the nice interface, I decided to make a slider which you can pull around. I didn't get too far... :D :( I just tried to make it stop at a point (the ends of the slides) when the mouse went too far. Somehow I didn't have success. I have a variable (slidemarkvar) which is set to 2 at the mouse-button-down event, and is set back to 1 at the mouse-button-up event. On the Draw event of that slider thingy I have the following code:
Code: Select all
if(slidemarkvar==2)
{
    FollowMouse("slidermark", X_AXIS);
    if(xmouse > 45)
    {
        slidermark.x=45;
    }
    if(xmouse < -130)
    {
        slidermark.x= -130;
    }
}


But here's the problem: When I hold the mouse button down, it stays in one place (somewhere around 40), when I release the mouse button, it follows the mouse on the x axis as it should, but it doesn't stop at 45 and -130! What's the problem? Can anyone help, please!

Re: Slider's script

PostPosted: Sat Feb 02, 2008 11:17 pm
by zygoth
Here is what I would do...

if(slidemarkvar == 2 && slidermark.x <= 45 && slidermark.x >= 130)FollowMouse("slidermark", X_AXIS);

I don't know if you have to tell it to stop following the mouse or not...if you do, just have it stop at the mouse button up event.

Hope this helps

Re: Slider's script

PostPosted: Sun Feb 03, 2008 7:33 am
by Thanx
Hello again!
I've solved the problem, so I'm going to share the script:
The Mouse Button Down and Up events have not changed, but many changes in the OnDraw! :)
Code: Select all
if(slidemarkvar == 2)
{
    if(slidermark.x > 51)
    {
        slidermark.x = 51;
    }
    if(slidermark.x < -123)
    {
        slidermark.x = -123;
    }
 
 
    FollowMouse("slidermark", X_AXIS);
 
 
    if(x > -123 && x < -109)
    {
        slidermark.x = -123;
        choosen_number.textNumber = 2;
    }
    if(x > -108 && x < -80)
    {
        slidermark.x = -94;
        choosen_number.textNumber = 3;
    }
    if(x > -79 && x < -51)
    {
        slidermark.x = -65;
        choosen_number.textNumber = 4;
    }
    if(x > -50 && x < -22)
    {
        slidermark.x = -36;
        choosen_number.textNumber = 5;
    }
    if(x > -21 && x < 8)
    {
        slidermark.x = -7;
        choosen_number.textNumber = 6;
    }
    if(x > 7 && x < 37)
    {
        slidermark.x = 22;
        choosen_number.textNumber = 7;
    }
    if(x > 36 && x < 51)
    {
        slidermark.x = 51;
        choosen_number.textNumber = 8;
    }


}
if(slidemarkvar != 2)
{
       slidermark.x = slidermark.xprevious;
}


First if statement accures when the mouse-button is held down. It will follow the mouse, but if the marker wants to go too far, it will be set back to the end of the slider. The rest of the if statement tells the marker to go to specific places if within a small range of them, and then set the choosen number to the defined value, which is the choosen number of players.
But what happens if you release :?: Notice the last if statement in that code (below too):

Code: Select all
if(slidemarkvar != 2)
{
       slidermark.x = slidermark.xprevious;
}

This keeps the slider in place even when the mousebutton is released. (This if statement was which I was complicating SO much, when the answer was SO simple! :D )

Re: Slider's script

PostPosted: Sun Feb 03, 2008 8:25 am
by DilloDude
This can be done more simply:
Code: Select all
if (slidemarkvar == 2)
{
    int slidepos = xmouse - xscreen;
    if (slidepos > 51)
    {
        slidepos = 51;
    }
    else if (slidepos < -123)
    {
        slidepos = -123;
    }
    slidermark.x = slidepos;
}

Adjust the values used.
Also, it's more conventional (and more practical) to use 0 (false) and 1 (true) for a variable used as a boolean.