SFML community forums
Help => General => Topic started by: Petre2710 on January 03, 2020, 09:23:35 pm
-
So I am trying to make a sprite to stay on screen, when my mouse is off-screen, and when the button is pressed.
The code:
Texture Temp;
Temp.loadFromFile("./Tiles/Boat.png");
Sprite Boat(Temp);
Boat.setScale(1.0, 1.0);
Event Action;
Keyboard Tastatura;
while (Game.isOpen())
{
while (Game.pollEvent(Action))
{
if (Action.type == Action.Closed)
{
Game.close();
}
if (Action.key.code==Tastatura.Space )
{
Boat.setPosition(static_cast<sf::Vector2f>(MousePoz.getPosition(Game)));
}
}
Game.clear(Color(139,0,139,255));
Game.draw(Boat);
Game.display();
}
Basically, when I press space, the sprite moves to my mouse position relative to the Game Window.
the problem is that when my moves out of the game window and space is pressed, the sprite moves off-screen too...
The boat is a small picture, and it loads correctly.
What can I do?
PS: Sorry if I made spelling mistakes.
-
You can just check to make sure the mouse position is within the size of the window before setting the boat's position to it.
-
You can just check to make sure the mouse position is within the size of the window before setting the boat's position to it.
I haven't thought of that... Thanks :)