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.