Question

I move sprites stored in a formationvector by holding the mousebutton. Problem is: Whenever i move the sprite onto another one, i move both sprites at the same time.

What i want: Moving the sprite1 over the other sprites2 without changing the position of sprite 2 or with other words:

-Testing in a loop if a sprite of a vector is clicked on

-If sprite is clicked on:

Move this sprite around while button is pressed but somehow stop this move while this movement in order to avoid moving more than this one sprite.

Here is my try so far:

while (App.pollEvent(Event))
    {
        // Window closed
        if (Event.type == sf::Event::Closed)
        {
            return (-1);
        }

        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {

            for (size_t k = 0; k < formation.size(); k++)
            {
                if (isMouseOver(formation[k], App) == true)
                {
                    Mouseposition = sf::Vector2f(sf::Mouse::getPosition(App));
                    Mouseposition.x = Mouseposition.x - formation[k].getLocalBounds().width / 2;
                    Mouseposition.y = Mouseposition.y - formation[k].getLocalBounds().height / 2;
                    formation[k].setPosition(sf::Vector2f(Mouseposition));
                    Formation_playernames.clear();
                    Formation_playerinformation.clear();
                    Formation_Playernames(Font, Formation_playernames, formation, playerlist);
                    Formation_Playerinformation(Font, Formation_playerinformation, formation, playerlist);
                }                                   
             }
         }
     }

The Formation-functions store the correct new positions of the sprites and their colors based on the positions into the formation vector The problem is the for-loop, i could do it without but that would lead to a much longer code. How could i do it ?

Was it helpful?

Solution

Instead of simply checking whether or not the mouse button is pressed, you should structure your code so you can check exactly when the button is pressed, and when it is released. You can then structure your code such that different presses of the button are distinguished from each other. semi-pseudocode follows

bool button_is_pressed = false;
Sprite* currently_selected_sprite = nullptr;

// main application loop
while (...)
{
    ...
    // other application logic
    ...

    if (!button_is_pressed)
    {
        if (CheckIfButtonIsPressed())
        {
            button_is_pressed = true;

            // Button was just pressed.
            // Select the appropriate sprite by checking
            // the mouse coordinates against the positions
            // of the sprites.
        }
        else
        {
            // Button not being pressed.
            // Likely no logic needed here.
        }
    }
    else // button_is_pressed == true
    {
        if (CheckIfButtonIsPressed())
        {
            // Button is being held down.
            // Implement dragging logic using the
            // pointer to the selected sprite.
        }
        else
        {
            button_is_pressed = false;
            // Button was just released.
            // Deselect the sprite.
            currently_selected_sprite = nullptr;
        }
    }    
}

Alternatively, you could handle mouse events, which will take care of much of this logic for you. http://sfml-dev.org/documentation/2.0/classsf_1_1Event.php

In a more English like pseudo code, this is what your function is doing:

if the mouse button is currently pressed
    move all the sprites which are under the mouse to be centered on the mouse cursor
else
    do nothing

This is what I'm suggesting

at the moment the mouse button is pressed down
    select the sprite which is under the mouse cursor
at the moment the mouse button is released
    deselect the selected sprite

if the mouse button is currently pressed, and a sprite is selected
    move the selected sprite to the position of the mouse cursor
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top