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

Author Topic: How can I stop my sprite from continuously to the right?  (Read 6371 times)

0 Members and 3 Guests are viewing this topic.

lundchoos

  • Guest
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.

coolp_jim

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: How can I stop my sprite from continuously to the right?
« Reply #1 on: May 03, 2016, 10:16:21 pm »
Do you ever reset your velocity.x to 0?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How can I stop my sprite from continuously to the right?
« Reply #2 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;
}

Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

lundchoos

  • Guest
Re: How can I stop my sprite from continuously to the right?
« Reply #3 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?

lundchoos

  • Guest
Re: How can I stop my sprite from continuously to the right?
« Reply #4 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.

coolp_jim

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: How can I stop my sprite from continuously to the right?
« Reply #5 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.

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: How can I stop my sprite from continuously to the right?
« Reply #6 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How can I stop my sprite from continuously to the right?
« Reply #7 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*