Hi
I decided to try and create a function that draws a button (relative to a window), and when it is pressed, it returns either true or false to a Boolean (and also moves the 'slider'). However, I could of just made a stupid mistake, but I can't seem to move anything when it is in a function. I know it cant be the code itself, as tried moving the rectangle shape in different places of the function. Please bear in mind I don't have a lot of experience with functions, so sorry if it doesn't make much sense. To clarify, the slider (rectangleShape) or any shape does not move as intended.
void buttonSlider(int xcord, int ycord, sf::RenderWindow& windowName)
{
bool active = false;
sf::Vector2i mousepos = sf::Mouse::getPosition(windowName);
sf::RectangleShape bbox(sf::Vector2f(50, 20));
sf::RectangleShape bslider(sf::Vector2f(25, 20));
bbox.setPosition(xcord, ycord);
bslider.setPosition(xcord, ycord);
bslider.setFillColor(sf::Color::Red);
int xplus50 = xcord + 50;
int yplus20 = ycord + 20;
int sliderCordX = bbox.getPosition().x;
int sliderCordY = bbox.getPosition().y;
windowName.draw(bbox);
windowName.draw(bslider);
if (mousepos.x >= xcord && mousepos.x < xplus50 && mousepos.y >= ycord && mousepos.y < yplus20 && sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
if (active == false && sf::Mouse::isButtonPressed(sf::Mouse::Left)){
bslider.move(25, 0);
active = true;
}
if (active == true && sf::Mouse::isButtonPressed(sf::Mouse::Left)){
bslider.move(-25, 0);
active = false;
}
}
}
http://en.sfml-dev.org/forums/index.php?topic=5559.0
Also, a few notes about the code:
if (mousepos.x >= xcord && mousepos.x < xplus50 && mousepos.y >= ycord && mousepos.y < yplus20 && sf::Mouse::isButtonPressed(sf::Mouse::Left))
I would change this to a sf::Rect<int>, and this if statement would be a lot cleaner, thus becoming:
if(rect.contains( mousepos.x, mousepos.y ) && sf::Mouse::isButtonPressed(sf::Mouse::Left))
void buttonSlider(int xcord, int ycord, sf::RenderWindow& windowName)
The variable named "windowName" doesn't make sense here. Also, with the coordinates, passing a sf::Vector<int> would be easier to read in my opinion.
windowName.draw(bbox);
windowName.draw(bslider);
if (mousepos.x >= xcord && mousepos.x < xplus50 && mousepos.y >= ycord && mousepos.y < yplus20 && sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
if (active == false && sf::Mouse::isButtonPressed(sf::Mouse::Left)){
bslider.move(25, 0);
active = true;
}
if (active == true && sf::Mouse::isButtonPressed(sf::Mouse::Left)){
bslider.move(-25, 0);
active = false;
}
}
Indentation could use a little work, but it seems like a copy-paste error. Any particular reason that you are moving a box after you draw it, and then it gets destroyed? If you don't understand what I mean, read up about RAII (https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization).
it returns either true or false to a Boolean
void buttonSlider(int xcord, int ycord, sf::RenderWindow& windowName)
Don't contradict yourself.
Please bear in mind I don't have a lot of experience with functions
Learn about functions, then. This function that you posted doesn't do anything like you think it should. If it still doesn't make sense to you, learn about scopes. This question is nothing (aside from using the library) to do with SFML, and there are better forums to ask these kinds of question to.