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

Author Topic: Reading position of shape  (Read 1216 times)

0 Members and 1 Guest are viewing this topic.

kusu

  • Newbie
  • *
  • Posts: 2
    • View Profile
Reading position of shape
« on: June 19, 2013, 12:29:41 am »
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. ;)
« Last Edit: June 19, 2013, 12:33:33 am by kusu »

MrMuffins

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Reading position of shape
« Reply #1 on: June 19, 2013, 01:25:29 am »
Because your getting the players position, which when you create the player, at 0.

Then you try to get the players position, but you never updated the position. Its still 0.
Put the posGreen either inside the loop or when you press a key.

kusu

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Reading position of shape
« Reply #2 on: June 19, 2013, 04:03:22 pm »
Thanks, now everything work. :)