SFML community forums

Help => General => Topic started by: lundchoos on May 03, 2016, 08:44:08 pm

Title: How can I stop my sprite from continuously to the right?
Post by: lundchoos on May 03, 2016, 08:44:08 pm
I trying to make a platforming game in which I do basic input like going right,left,jump etc.

Main.cpp (only the while loop)

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

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
    player.MoveRight();
}

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
    player.MoveLeft();
}

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
    player.Jump();
}

player.MovePlayer();
window.clear();
window.draw(player.GetShape());
window.display();
}

Player.h

#pragma once
#include <SFML/Graphics.hpp>
class Player
{
private:
 float speed = 5.0f;
  float gravity = 3.0f;
  float jumpSpeed = 2.0f;
  sf::Vector2f velocity;
  sf::Vector2f position;
public:
  Player(); //Constructor
  sf::RectangleShape playerShape;
  sf::RectangleShape GetShape();
  void MoveLeft();
  void MoveRight();
  void Jump();
  void MovePlayer();
};

Player.cpp

#include <SFML/Graphics.hpp>
#include <iostream>
#include "Player.h"
Player::Player() {
  playerShape.setFillColor(sf::Color::Green);
  playerShape.setSize(sf::Vector2f(100.0f, 100.0f));
  playerShape.setPosition(sf::Vector2f(10.0f, 350.0f));
}

sf::RectangleShape Player::GetShape() {
  return playerShape;
}

void Player::MoveLeft() {
  velocity.x = speed;
}

void Player::MoveRight() {
  velocity.x = speed;
}

void Player::Jump() {
  velocity.y = jumpSpeed;
}

void Player::MovePlayer() {
  position.x += velocity.x;
  position.y += velocity.y;
  playerShape.move(position.x, position.y);
}

When I press suppose the right key but when I release it the sprite doesnt stop it goes on moving in that direction. How do I stop this.
Title: Re: How can I stop my sprite from continuously to the right?
Post by: coolp_jim on May 03, 2016, 10:16:21 pm
Do you ever reset your velocity.x to 0?
Title: Re: How can I stop my sprite from continuously to the right?
Post by: Hapax on May 03, 2016, 10:55:12 pm
Please post your code in [code=cpp] [/code] tags.

You probably want MoveLeft to set the velocity to a negative value thus:
void Player::MoveLeft() {
  velocity.x = -speed;
}

Title: Re: How can I stop my sprite from continuously to the right?
Post by: lundchoos on May 04, 2016, 01:03:18 pm
Please post your code in [code=cpp] [/code] tags.

You probably want MoveLeft to set the velocity to a negative value thus:
void Player::MoveLeft() {
  velocity.x = -speed;
}



How can I do that?
Title: Re: How can I stop my sprite from continuously to the right?
Post by: lundchoos on May 04, 2016, 01:06:29 pm
Please post your code in [code=cpp] [/code] tags.

You probably want MoveLeft to set the velocity to a negative value thus:
void Player::MoveLeft() {
  velocity.x = -speed;
}



Yes, that was an error However that still did not solve my problem that once I press any key my sprite moves but when I release the key the sprite doesnt stop, it keeps going in that direction.
Title: Re: How can I stop my sprite from continuously to the right?
Post by: coolp_jim on May 04, 2016, 02:11:18 pm
Do you ever reset your velocity.x to 0?

Maybe this was too subtle..  ;) But yeah, you need to set your velocity to 0 in your main loop however you want to do it, before your isKeyPressed code.
Title: Re: How can I stop my sprite from continuously to the right?
Post by: Erdrick on May 04, 2016, 02:13:12 pm
Have the code inside MoveRight increment position.x and not velocity.x then.  Velocity as its definition suggests, sets a constant speed.  Its like setting cruise control on your car.
Title: Re: How can I stop my sprite from continuously to the right?
Post by: Hapax on May 09, 2016, 12:42:10 am
I thought you were aiming for force/thrust (adjusting velocity to change rate of movement is a direction) rather than absolute direction (setting a specific velocity in a specific direction).

If absolute direction, you need to reset your velocity to zero to make it stop. If you don't, you need to press the opposite direction to slow it down.
You could reset it before assigning a velocity or just reset it when you release a key. The former (reset always) is probably the simpler and possibly better option.