Hi!
I just start learning sfml and stuck with little problem.
When i try read coordinates from player green, i always have 0 in console.
Maybe someone can help me, here's the code. : )
Thanks.
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <SFML\System.hpp>
#include <iostream>
using namespace std;
class player
{
public:
sf::RectangleShape pl;
player(int r, int g, int b, float w, float h)
{
pl.setSize(sf::Vector2f(w, h));
pl.setFillColor(sf::Color(r, g, b));
}
};
int main()
{
sf::RenderWindow okno(sf::VideoMode(1024, 768), "SFML", sf::Style::Close);
okno.setKeyRepeatEnabled(true);
okno.setMouseCursorVisible(false);
player green(250, 250, 250, 15, 15);
sf::Vector2f posGreen = green.pl.getPosition();
while(okno.isOpen())
{
sf::Event event;
while(okno.pollEvent(event))
{
if(event.type == sf::Event::Closed){ okno.close(); }
if(event.type == sf::Event::KeyPressed)
{
if(event.key.code == sf::Keyboard::Escape)
{
okno.close();
}
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
green.pl.move(0, -5);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
green.pl.move(-5, 0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
green.pl.move(0, 5);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
green.pl.move(5, 0);
}
cout << "x = " << posGreen.x << endl;
cout << "y = " << posGreen.y << endl;
system("cls");
okno.clear();
okno.draw(green.pl);
okno.display();
}
return 0;
}
Ps. I know that system("cls") isn't good practice.