SFML community forums

Help => General => Topic started by: jancsika998 on November 26, 2021, 10:38:16 am

Title: if position with sfml
Post by: jancsika998 on November 26, 2021, 10:38:16 am
I want to ask for help that I don't know how to use if for position in sfml.
For example, if I want something to go as long as it is given inside the if.

I have this code:

#include <SFML\Graphics.hpp>
#include<SFML\Audio.hpp>
#include<SFML\Network.hpp>

int main{
sf::RenderWindow window(sf::VideoMode(1080, 600), "sfml tutorial", sf::Style::Close | sf::Style::Resize);
sf::View Player1(sf::Vector2f(-32.f, 0.f), sf::Vector2f(512.f, 512.f));
sf::RectangleShape slot1(sf::Vector2f(64.f, 128.f));

slot1.setPosition(-264.f, 128.f);

while(window.isOpen())
        {
               
                sf::Event evnt;
               
               
                while(window.pollEvent(evnt))
                {
                        if(evnt.type == evnt.Closed)
                        {
                                window.close();
                        }
                }
window.draw(slot1);
window.display();
                }
               
       
        }
       
        return 0;
}
I want make one animation with this Rectangle, while something Dont true.

Sorry for my english.

Title: Re: if position with sfml
Post by: Stauricus on November 26, 2021, 11:54:43 am
I didn't know for sure if I got what you mean by "something to go as long as it is given inside the if".


do you want to move the slot1 to move around when you press some key on the keyboard? if that's it, check this:
https://www.sfml-dev.org/tutorials/2.5/window-inputs.php#keyboard

OBS.: you forgot to clear() the window in your code.
Title: Re: if position with sfml
Post by: jancsika998 on November 26, 2021, 06:22:55 pm
for example

if(player position <= object position)
{
  do anything.
}
Title: Re: if position with sfml
Post by: Stauricus on November 26, 2021, 08:12:17 pm

while (player position <= object position)
{
  //do anything
}

this is not really SFML related... have a check on some basic tutorials about loops and conditions (do, for, while)...