Stuck with being unable to move individual objects inside of an array of 2d sprites?

  2d-games, c++

So I’m stuck on this bug I’ve had with a game I’ve been working on for a while now and am unsure of what to do!
I have a array of sprites named friendly_block, and what I’m trying to achieve is moving the individual sprites by taking the x and y location of the mouse cursor and checking if its inside of the friendly_block sprite. If it is, then it does another check to see if I’m clicking. I use a boolean named "clicking" that is true when the left mouse button is being held down.
Now it actually works but the problem I’m having is that I cant make the program so that when I’m clicking on one sprite, I cannot start dragging other sprites. Currently what happens is when I click on one and drag, if I go near another friendly_block then it will also pick up that one and set its x and y pos to the cursor meaning that two blocks become stuck on top of each other.

Below is the code for the dragging of the array of sprites:

const auto* movement = dynamic_cast<const ASGE::MoveEvent*>(data.get());
double mouse_x       = movement->xpos;
double mouse_y       = movement->ypos;

Logging::TRACE("Mouse X: " + std::to_string(mouse_x));
Logging::TRACE("Mouse y: " + std::to_string(mouse_y));

for (size_t i = 0; i < 10; ++i)
{
  if (isInsideBlock(friendly_block[i], static_cast<float>(mouse_x), static_cast<float>(mouse_y)))
 {
  if (clicking)
  {
    friendly_block[i]->xPos(static_cast<float>(mouse_x - 25));
    friendly_block[i]->yPos(static_cast<float>(mouse_y - 25));
  }
  else
  {
    friendly_block[i]->xPos(friendly_block[i]->xPos());
    friendly_block[i]->yPos(friendly_block[i]->yPos());
  }
}

Please let me know what any of you think! 🙂
I understand the logic of why it’s doing it but I cant seem to find a way to stop it and I’m stumped!

Source: Windows Questions C++

LEAVE A COMMENT