O'Reilly logo

SFML Game Development By Example by Raimondas Pupius

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 << ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required