I have a class which should display a sprite at a different position and rotation depending on input, but for some reason the sprite is only drawn when I input "up".
The way the class works is that the constructor is given a pointer to the texture, a pointer to the player which it is supposed to follow, the direction it is supposed to face, and the index that it will have in the vector of draw-able objects. In the constructor there is an if else if to check if the direction is up, down, left, or right. The code in each of these is identical except the rotation and position given is different but the sprite will only draw if UP is true. It does not matter what order the if elses are in, or what values are given to rotation and position, even if they are all completely identical it will only draw the sprite if the direction is UP. It is not a case of it not going into the if statements as I have checked that through debugging and the positions and rotations are set correctly.
It makes no sense to me and I'm pulling my hair out because of it. It is certainly something I am doing wrong but I just can't figure it out. I'll include all the code I think is relevant let me know if any more is needed.
Slash class where the problem lies:
class Slash : public Drawable
{
public:
Slash(){}
Slash(sf::Texture*, Drawable*, Direction, int i);
sf::Sprite sprite;
sf::Clock clock;
Drawable* parent;
void update(float t);
};
Slash::Slash(sf::Texture* t,Drawable* pParent, Direction d, int i) : Drawable(t,pParent->getPosition(),i)
{
parent = pParent;
direction = d;
if(direction == LEFT)
{
spritesheet.sprite.setRotation(90.0f);
spritesheet.sprite.setPosition(sf::Vector2f(position.x + 95,position.y - 25));
}
else if(direction == UP)
{
spritesheet.sprite.setRotation(90.0f);
spritesheet.sprite.setPosition(sf::Vector2f(position.x + 95,position.y - 25));
}
else if(direction == DOWN)
{
spritesheet.sprite.setRotation(-90.0f);
spritesheet.sprite.setPosition(sf::Vector2f(position.x - 30, position.y + 150));
}
else if(direction == RIGHT)
{
spritesheet.sprite.setRotation(180.0f);
spritesheet.sprite.setPosition(sf::Vector2f(position.x + 100, position.y + 130));
}
clock.restart();
}
void Slash::update(float t)
{
position = parent->getPosition();
if(direction == UP)
{
spritesheet.sprite.setPosition(sf::Vector2f(position.x + 95,position.y - 25));
}
else if(direction == LEFT)
{
spritesheet.sprite.setPosition(sf::Vector2f(position.x + 95,position.y - 25));
}
else if(direction == DOWN)
{
spritesheet.sprite.setPosition(sf::Vector2f(position.x - 30, position.y + 150));
}
else if(direction == RIGHT)
{
spritesheet.sprite.setPosition(sf::Vector2f(position.x + 100, position.y + 130));
}
if(clock.getElapsedTime().asMilliseconds() >= 500)
{
allDrawables.erase(allDrawables.begin()+index);
delete this;
}
}
The "Drawable" parent class:
class Drawable
{
public:
Drawable(sf::Texture* t, sf::Vector2f, int i);
Drawable(){}
virtual ~Drawable(){}
float depth;
sf::Sprite* getSprite();
sf::Vector2f getPosition();
inline virtual void update(float t){}
int index;
sf::Clock clock;
protected:
sf::Texture texture;
Spritesheet spritesheet;
Direction direction;
sf::Vector2f position;
};
Drawable::Drawable(sf::Texture* t,sf::Vector2f pPos, int i)
{
spritesheet = Spritesheet(t,128,64,4);
index = i;
position = pPos;
}
sf::Vector2f Drawable::getPosition()
{
return position;
}
sf::Sprite* Drawable::getSprite()
{
return spritesheet.getSprite(direction);
}
My Spritesheet class:
class Spritesheet
{
public:
Spritesheet(){}
Spritesheet(sf::Texture* texture, unsigned int rectH, unsigned int rectW, unsigned int spriteNo);
sf::Sprite* getSprite(unsigned int index);
sf::Sprite sprite;
private:
sf::IntRect rect;
unsigned int spriteNo;
unsigned int sprIndex;
unsigned int animIndex;
};
Spritesheet::Spritesheet(sf::Texture* texture,unsigned int recth, unsigned int rectw, unsigned int spriteno)
{
rect.height = recth;
rect.width = rectw;
rect.top = 0;
rect.left = 0;
sprite.setTextureRect(rect);
sprite.setTexture(*texture);
spriteNo = spriteno;
sprIndex = 0;
}
sf::Sprite* Spritesheet::getSprite(unsigned int index)
{
rect.left = rect.width * index;
sprite.setTextureRect(rect);
return &sprite;
}
And the code where the "Slash" objects are created:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
direction = UP;
if(attClock.getElapsedTime().asMilliseconds() > 1000)
{
Slash* s = new Slash(&allTextures[2],this,direction,allDrawables.size());
allDrawables.push_back(s);
attClock.restart();
}
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
direction = DOWN;
if(attClock.getElapsedTime().asMilliseconds() > 1000)
{
Slash* s = new Slash(&allTextures[2],this,direction,allDrawables.size());
allDrawables.push_back(s);
attClock.restart();
}
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
direction = LEFT;
if(attClock.getElapsedTime().asMilliseconds() > 1000)
{
Slash* s = new Slash(&allTextures[2],this,direction,allDrawables.size());
allDrawables.push_back(s);
attClock.restart();
}
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
direction = RIGHT;
if(attClock.getElapsedTime().asMilliseconds() > 1000)
{
Slash* s = new Slash(&allTextures[2],this,direction,allDrawables.size());
allDrawables.push_back(s);
attClock.restart();
}
}
allTextures is a vector containing all my textures, which are loaded when the program starts, allDrawables is a vector of pointers to each of the objects to be drawn. Thank you in advance for any help given.