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

Author Topic: Changing screen when player is outside window.  (Read 1302 times)

0 Members and 1 Guest are viewing this topic.

Bobson

  • Newbie
  • *
  • Posts: 3
    • View Profile
Changing screen when player is outside window.
« on: March 26, 2014, 07:09:42 pm »
I am creating my first game (for learning purposes) and its based on text commands. If i for example tell my player to move to the right screen i want him to move until he reaches the border of the screen and then switch to the next screen. See images for example.

http://imgur.com/RHBisbz
then i enter move right
http://imgur.com/Yd3dhcO

Right now the screen switches instantaneously as i type the command. I am trying to make a move function where i want him to move to the right and when he reaches the end the window the screen will switch. My question right now is how can i make him move to a vector (x,y) position over a certain time? I know i can change his position with mPlayer.setPosition(sf::vector2f) but then he will move instant and not over a certain time.

I have tested some with this code:

void Game::update(sf::Time deltaTime)
{
if (GoUp)
        {
                Spelare.mPlayer.setPosition(sf::Vector2f((Spelare.mPlayer.getPosition().x),(Spelare.mPlayer.getPosition().y-0.3)));
                Spelare.source.y = Spelare.Up;
        }

        if (GoDown)
        {
                Spelare.mPlayer.setPosition(sf::Vector2f((Spelare.mPlayer.getPosition().x),(Spelare.mPlayer.getPosition().y+0.3)));
                Spelare.source.y = Spelare.Down;
        }

        if (GoLeft)
        {
                Spelare.mPlayer.setPosition(sf::Vector2f((Spelare.mPlayer.getPosition().x-0.8),(Spelare.mPlayer.getPosition().y)));
                Spelare.source.y = Spelare.Left;
        }

        if (GoRight)
        {
                Spelare.mPlayer.setPosition(sf::Vector2f((Spelare.mPlayer.getPosition().x+0.8),(Spelare.mPlayer.getPosition().y)));
                Spelare.source.y = Spelare.Right;
        }
}

This is 4 different booleans for each direction so if i want him to move to a location i can set for example goUp and GoLeft to true but he will never stop and the code is ugly.

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Changing screen when player is outside window.
« Reply #1 on: March 26, 2014, 07:23:41 pm »
Reading the documentation helps a lot ;)

To stop him after a while you can place a rectangle as large as your screen and then use sf::Rect<T>::intersects or sf::Rect<T>::contains to know whether he is off the screen. Or just do a manual check for his position.