1
General / 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.
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.