在矢量中移动精灵

Moving sprites in a vector

本文关键字:移动 精灵      更新时间:2023-10-16

我通过按住鼠标按钮移动存储在formationvector中的精灵。问题是:每当我移动一个精灵到另一个精灵上时,我同时移动了两个精灵。

我想要的:移动sprite1到其他sprites2上,而不改变sprite1的位置或使用其他文字:

-在循环中测试如果一个矢量的精灵被点击

-如果精灵被点击:

当按钮被按下时移动这个精灵,但是为了避免移动多个精灵,在这个移动过程中以某种方式停止这个移动。

这是我到目前为止的尝试:

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);
                }                                   
             }
         }
     }

生成函数存储精灵的正确新位置和基于位置的颜色到生成向量中问题是for循环,我可以不这样做,但这会导致更长的代码。

不是简单地检查鼠标按钮是否被按下,而是应该构建代码,以便准确地检查按钮何时被按下以及何时被释放。然后,您可以构建代码,以便区分不同的按钮按下。semi-pseudocode遵循

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;
        }
    }    
}

或者,您可以处理鼠标事件,它将为您处理大部分逻辑。http://sfml-dev.org/documentation/2.0/classsf_1_1Event.php

在一个更像英语的伪代码中,这就是你的函数所做的:

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

这就是我的建议

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