0
votes

I created a class for movement of a sprite along with it's animation,pretty basic stuff.

After finally removing all the error and all I checked through the underlying logic and all and saw that the movement vector was pretty much what i wanted and everything seemed alright.The problem is once I press the buttons for the movement the sprite moves like for a second and then returns to it's original position.

My class move(responsible for the movement and animation of the sprite) returns a sprite which is taken in the main source code which moves according to a variable set in move.

If I set the variable to a high enough value then I can notice the sprite moving about it's original position for an instant and then return and then move again and return.I have checked the code.I have not reset the sprite position or something like that. Here's the code:-

Source code:-

#include"SFML\Graphics.hpp"
#include"check.h"
#include"display.h"
int main()
{
sf::Sprite plop;
sf::RenderWindow window(sf::VideoMode(1360, 720), "Larger SFML", sf::Style::Default);
sf::Texture texture;
texture.loadFromFile("bahamut.png");
texture.setRepeated(false);
float fraps = 0.0;
check playa;
sf::Clock fps;
while (window.isOpen())
{
    fraps = fps.restart().asSeconds();
    plop = playa.movereturn(100000.,fraps,&texture);  
    window.clear();
    window.draw(plop);
    display dis(window);
}

return 0;

}

Here is the header for check:-

    #pragma once
#include"SFML\Graphics.hpp"
class check
{
public:
    check();
    sf::Sprite movereturn(float speed,float fps,sf::Texture* texture);
    ~check();
};

Here is the definition of check:-

#include "check.h"



check::check()
{
}

sf::Sprite check::movereturn(float speed,float fps,sf::Texture* texture)
{
    sf::Sprite playas;
    playas.setTexture(*texture);
    sf::Vector2f movements = { 0.,0. };
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
    {
        movements = { -speed*fps,0. };
        playas.move(movements);
    }
    else
    {
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
        {
            movements = { speed*fps, 0. };
            playas.move(movements);
        }
        else
        {
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
            {
                movements = { 0.,speed*fps };
                playas.move(movements);
            }
            else
            {

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
                {
                    movements = { 0.,-speed*fps };
                    playas.move(movements);
                }
                else
                    movements = { 0., 0. };

            }
        }
    }
    return playas;

}


check::~check()
{
}

Display just takes in the window and has window.display() function in it.Without this class there's a handler exception so I'm forced to use this.

1
You should be able to just call window.display() without needing your display class, you should also update your other question with the issues you are having, since this is basically the same code. - AresCaelum

1 Answers

0
votes

Alright so a few issues with your code.

  1. You should use the correct syntax for a float. 0.0f is for a float, 0.0 is for a double. Surprise your compiler isn't giving you a warning about that.
  2. Your Sprite is being remade every frame in your movereturn function, so yea your sprite is only moving alittle to whatever direction your telling him to go. The very next frame it is remade with its default values (Which is a position of 0.) if you were using the built in visual studio debugger you would see that every frame your sprite playas position was always reset to its default which should be a huge hint to your issue.
  3. Since you are limiting it so a player can only move 1 direction, they cant move diagonally(Up Left) you don't need all of your else statements.
  4. Always, use brackets regardless if its a 1 line if statement always use brackets. you are allowed to get away with it but it makes debugging much simpler when you use brackets.

This should fix your issue.

Check.h

#pragma once
#include"SFML\Graphics.hpp"
class check
{
    sf::sprite playas;
public:
    check();
    sf::Sprite movereturn(float speed,float fps,sf::Texture* texture);
    ~check();
};

Check.cpp

sf::Sprite check::movereturn(float speed,float fps,sf::Texture* texture)
{
    playas.setTexture(*texture);
    sf::Vector2f movements = { 0.0f,0.0f };
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
    {
        movements = { -speed*fps,0.0f };
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
    {
        movements = { speed*fps, 0.0f };        
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
    {
        movements = { 0.0f,speed*fps };
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
    {
        movements = { 0.0f,-speed*fps };
    }
    playas.move(movements);
    return playas;
}

If you wanted diagonal movement you can change your code to this in your movereturn function:

sf::Sprite check::movereturn(float speed,float fps,sf::Texture* texture)
{
    playas.setTexture(*texture);
    sf::Vector2f movements = { 0.0f,0.0f };
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
    {
        movements.x = -speed*fps;
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
    {
        movements.x = speed*fps;        
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
    {
        movements.y = speed*fps;
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
    {
        movements.y = -speed*fps;
    }
    playas.move(movements);
    return playas;

}