在矢量中移动精灵
Moving sprites in a vector
我通过按住鼠标按钮移动存储在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
相关文章:
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- SFML无法正确移动精灵
- 在鼠标位置移动精灵
- 精灵不在SFML中移动
- 减慢精灵的移动速度
- 将聚焦的精灵逐渐移动到目标位置(C++和SFML)
- 如何顺利移动精灵
- 在 Cocos2DX 中移动动画精灵和路径
- SFML在另一个类中绘制移动精灵
- 当我移动我的精灵时,它一直从窗户掉下来
- 以相同的速度将精灵从一个点移动到另一个点
- 移动精灵和处理事件
- 用VBO和FBO绘制一个移动的精灵
- c++停止敌人精灵相互移动
- 精灵和项目符号不移动C++
- SDL2.0移动精灵与键盘
- C++ DirectX11 2d 游戏:阻止敌人精灵相互移动
- 在矢量中移动精灵
- 防止将一个精灵移动到另一个精灵上