Shawn S.
Shawn S.

Reputation: 97

How to update the cursor when I use mousewheel?

My problem is that when I move the view with the mousewheel, the cursor sprite stays behind and it doesn't update to the cursor itself, how do I fix this?

Main Game Loop:

this->cursor->Update(window);

if (event->type == sf::Event::MouseWheelMoved)
{
    if (event->mouseWheel.delta == 1)
    {
        this->view->move(sf::Vector2f(0, -25));
        window->setView(*this->view);
        event->mouseWheel.delta = 0;
    }
    else if (event->mouseWheel.delta == -1)
    {
        this->view->move(sf::Vector2f(0, 25));
        window->setView(*this->view);
        event->mouseWheel.delta = 0;
    }
}

Cursor Update Function:

this->setPosition(static_cast<sf::Vector2f>(sf::Mouse::getPosition(*window)));

Github Link: Capitalist Adventure - Project In Question

Upvotes: 0

Views: 125

Answers (1)

Shawn S.
Shawn S.

Reputation: 97

After looking at the SFML Forums I have found an answer to why the set sprite position function wasn't setting my sprite cursor to my mouse.The reason was that the sf::View messes around with the coordinates on-screen.

Answer:

this->setPosition(window->mapPixelToCoords(sf::Mouse::getPosition(*window)));

Upvotes: 1

Related Questions