Witam,
Napisałem grę w której obiekt powinien się poruszać tylko i wyłącznie w czterech kierunkach (góra, dół, prawo, lewo). Niestety gdy puszczam jedna strzałkę i szybko naciskam inną obiekt porusza się na ukos.
Byłbym wdzięczny za podpowiedź jak rozwiązać mój problem. Z góry dziękuję za wszystkie rady
Pozdrawiam.
kwadrat.h
#ifndef KWADRAT_H
#define KWADRAT_H
#include <SFML/Graphics.hpp>
using namespace sf;
class kwadrat : public sf::Drawable
{
public:
kwadrat(float X, float Y);
~kwadrat() = default;
void update();
RectangleShape Shape;
const float length{75.0f};
const float height{50.0f};
private:
void draw(RenderTarget& target, RenderStates state) const override;
const float rectanglevelocity{1.f};
Vector2f Velocity{rectanglevelocity, rectanglevelocity};
};
#endif
kwadrat.cpp
include "kwadrat.h"
#include <SFML/Graphics.hpp>
#include <windows.h>
using namespace sf;
kwadrat::kwadrat(float X, float Y)
{
Shape.setPosition(X, Y);
Shape.setSize({this->length, this->height});
Shape.setFillColor(Color::White);
Shape.setOrigin(this->length / 2, this->height / 2);
void kwadrat::draw(RenderTarget& target, RenderStates state) const
{
target.draw(this-> Shape, state);
}
void kwadrat::update()
{
Shape.move(this->Velocity);
if(Keyboard::isKeyPressed(Keyboard::Key::Left))
{
Velocity.x = -rectanglevelocity;
Shape.setRotation( 270 );
}
else if((Keyboard::isKeyPressed(sf::Keyboard::Right))
{
Velocity.x = rectanglevelocity;
Shape.setRotation( 90 );
}
else if(Keyboard::isKeyPressed(Keyboard::Key::Up))
{
Velocity.y = -rectanglevelocity;
Shape.setRotation( 0 );
}
else if(Keyboard::isKeyPressed(Keyboard::Key::Down))
{
Velocity.y = rectanglevelocity;
Shape.setRotation( 180);
}
else
{
Velocity.x = 0;
Velocity.y = 0;
}
}