Moving a sprite revisited
Now that we have a fancy event manager, let's test it fully by moving a sprite to the location of the mouse when the left shift key is held down and the left mouse button is pressed. Add two new data members to your Game
class: m_texture
and m_sprite
. Set them up as discussed in the previous chapters. For our purposes, we'll just be re-using the mushroom graphic from the first few chapters. Now add and implement a new method in your game class called MoveSprite
:
void Game::MoveSprite(EventDetails* l_details){ sf::Vector2i mousepos = m_window->GetEventManager()->GetMousePos(m_window->GetRenderWindow()); m_sprite.setPosition(mousepos.x, mousepos.y); std::cout << "Moving sprite to: " << mousepos.x << ":" << mousepos.y << std::endl; }
What we do here is grab the mouse position relative to the current window from the event manager and store it in a local integer vector called mousepos
. We then set the position of our sprite...