Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sprite confined to screen--Help--Beginer  (Read 1680 times)

0 Members and 1 Guest are viewing this topic.

Petre2710

  • Newbie
  • *
  • Posts: 3
    • View Profile
Sprite confined to screen--Help--Beginer
« 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.
« Last Edit: January 03, 2020, 09:32:01 pm by Petre2710 »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Sprite confined to screen--Help--Beginer
« Reply #1 on: January 03, 2020, 10:52:11 pm »
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.

Petre2710

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite confined to screen--Help--Beginer
« Reply #2 on: January 04, 2020, 12:24:44 am »
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 :)

 

anything