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

Author Topic: Little questions/issue about view.move [SOLVED]  (Read 4144 times)

0 Members and 1 Guest are viewing this topic.

2ant

  • Newbie
  • *
  • Posts: 17
    • View Profile
Little questions/issue about view.move [SOLVED]
« on: February 24, 2015, 03:33:29 pm »
Hi,i'm learning both c++ and sfml and i did a simple camera movement with 4 keyboard keys.

sf::View view(sf::FloatRect(0, 0, 640, 360));

window.setView(view);

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))//move the camera up
                {
                    view.move(0, -32);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::X))//move the camera down
                {
                    view.move(0, 32);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))//move the camera left
                {
                    view.move(-32, 0);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::C))//move the camera right
                {
                    view.move(32, 0);
                }
 

My problem is that the camera move double the distance.

example : view.move(32, 0) will make the view/camera move 64 pixels to the right and not 32.

Is that normal ?
Why ?
« Last Edit: February 24, 2015, 05:17:57 pm by 2ant »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Little questions/issue about view.move
« Reply #1 on: February 24, 2015, 03:50:34 pm »
The keys will most likely remain pressed for more than one iteration of your game loop, so each key press will trigger multiple calls to move(). If you want to react to key press/release, and not to key down, use events.
Laurent Gomila - SFML developer

2ant

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Little questions/issue about view.move
« Reply #2 on: February 24, 2015, 04:27:02 pm »
Thank you for your fast answer, i forgot to post the "event" lines :

I have this:

sf::Event event;
            while (window.pollEvent(event))
            {
                if (event.type == sf::Event::Closed)
                    window.close();

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                {
                    view.move(0, -32);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::X))
                {
                    view.move(0, 32);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                {
                    view.move(-32, 0);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::C))
                {
                    view.move(32, 0);
                    title += " XX";
                    window.setTitle(title);
                }
 

I added this:
title += " XX";
window.setTitle(title);
And it briefly show four X each time the camera moves.

Should i post my entire (not so long) code ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Little questions/issue about view.move
« Reply #3 on: February 24, 2015, 04:48:08 pm »
Don't mix events and real-time input, it doesn't make sense. If you are inside an event loop, use the available event data.
Laurent Gomila - SFML developer

2ant

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Little questions/issue about view.move [SOLVED]
« Reply #4 on: February 24, 2015, 05:17:06 pm »
Thank you Laurent !

I don't even remember why i did it this way  ::)

while (window.pollEvent(event))
            {
                switch (event.type)
                {
                    case sf::Event::Closed:
                        window.close();
                        break;

                    case sf::Event::KeyPressed:
                        {
                            if (event.key.code == sf::Keyboard::S)
                                {
                                    view.move(0, -32);
                                }
                            else if (event.key.code == sf::Keyboard::X)
                                {
                                    view.move(0, 32);
                                }
                            else if (event.key.code == sf::Keyboard::W)
                                {
                                    view.move(-32, 0);
                                }
                            else if (event.key.code == sf::Keyboard::C)
                                {
                                    view.move(32, 0);
                                }