Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with moving stuff inside a function  (Read 2363 times)

0 Members and 3 Guests are viewing this topic.

Gauzr

  • Newbie
  • *
  • Posts: 20
    • View Profile
Problem with moving stuff inside a function
« on: July 12, 2015, 01:39:14 pm »
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;
                }
        }
}

 

kitteh-warrior

  • Guest
Re: Problem with moving stuff inside a function
« Reply #1 on: July 12, 2015, 04:09:06 pm »
http://en.sfml-dev.org/forums/index.php?topic=5559.0

Also, a few notes about the code:

Quote
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))

Quote
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.

Quote
    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.

Quote
it returns either true or false to a Boolean
Quote
void buttonSlider(int xcord, int ycord, sf::RenderWindow& windowName)

Don't contradict yourself.

Quote
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.
« Last Edit: July 12, 2015, 04:19:12 pm by kitteh-warrior »

Gauzr

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Problem with moving stuff inside a function
« Reply #2 on: July 12, 2015, 05:30:12 pm »
Hi kitteh-warrior
Thanks for the help. I perhaps should of checked my code a bit first, yes I do realise it doesn't return a Boolean, what I meant (or what I should of said) was itwill return one. I have taken in what you said, but what about the problem with moving a rectangle shape? I have since improved my code, but it still wont move (In honesty I have no idea why).

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with moving stuff inside a function
« Reply #3 on: July 12, 2015, 05:33:26 pm »
Gauzr, you're only moving the rectangle after its been drawn but even then, not returning the new coordinates so the calling code doesn't know it's been moved.

with the coordinates, passing a sf::Vector<int> would be easier to read in my opinion.
You probably mean sf::Vector2<int> or simply sf::Vector2i

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.
Aside from being right about moving it after drawing it then it getting destroyed, I don't see how directing towards RAII is the appropriate response. This is simply an object on the stack that is going out of scope.

You don't have to talk down to people because they are posting incorrectly; simply inform them and direct them where you think they ask that question instead of just saying "not here; bye".
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Gauzr

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Problem with moving stuff inside a function
« Reply #4 on: July 12, 2015, 05:41:28 pm »
Ah, moving the rectangle before drawing it solved my newbie problem. Thanks.

kitteh-warrior

  • Guest
Re: Problem with moving stuff inside a function
« Reply #5 on: July 12, 2015, 05:55:04 pm »
You probably mean sf::Vector2<int> or simply sf::Vector2i
Haha, yeah, sorry about that. I simply typed the idea of what I was thinking at the time :P

Quote
This is simply an object on the stack that is going out of scope.
That is what I meant, but I guess I confused myself.

Quote
You don't have to talk down to people because they are posting incorrectly; simply inform them and direct them where you think they ask that question instead of just saying "not here; bye".

I didn't disregard the problem posted in this thread, I attempted to help by referring more SFML related things (like sf::Vector2i), as well as what appears to be the wrong concept (RAII instead of scope). I tend to be a rather satirical sometimes, but I meant no offense. If I came off as rude, I apologise.

 

anything