I figured that if I put the intializations of the hero outside of the window.isOpen loop, it would make the character move, but if I remove the "hero.spriteSheetHero.mainSprite.setTexture(*((hero.spriteSheetHero.getVectorSpriteJ(2)).getTexture()));" it would make him disappear. Why is that?
Here is a code where you can move the character, also got one problem with my animation system : it only display one frame of the two
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <string>
#include <vector>
#define SIZE 12
sf::Event event;
class Spritesheet
{
public:
sf::Sprite mainSprite;
sf::Texture textureHero[SIZE];
int vectorPX[SIZE];
int vectorPY[SIZE];
void setVectorSprite(int vectorX[SIZE], int vectorY[SIZE]);
std::vector<sf::Sprite> & getVectorSprite();
sf::Sprite & getVectorSpriteJ(int j);
std::string filenameS;
std::vector<sf::Sprite> vectorSprite;
private:
//std::vector<sf::Sprite> vectorSprite;
};
class Hero
{
public:
Spritesheet spriteSheetHero;
void setHealth(int health);
int & getlastX();
int & getlastY();
void setPos(int posX, int posY);
private:
int healthH;
int lastposX, lastposY;
int curX, curY;
};
int main()
{
sf::Clock clock;
sf::Time elapse;
Hero hero;
hero.setHealth(100);
int testX[SIZE] = { 14, 49, 78, 109, 170, 360, 536, 695, 861, 1020, 1145, 1180 };
int testY[SIZE] = { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };
hero.spriteSheetHero.setVectorSprite(testX, testY );
sf::RenderWindow window(sf::VideoMode(800,600), "SFML Hero");
window.setFramerateLimit(30);
while(window.isOpen())
{
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
default:
break;
}
}
window.clear(sf::Color(250,127,0)); // orange = sf::Color(250,127,0)
hero.spriteSheetHero.mainSprite.setTexture(*((hero.spriteSheetHero.getVectorSpriteJ(2)).getTexture()));
// setting the herz
clock.restart();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
elapse = clock.getElapsedTime();
hero.spriteSheetHero.mainSprite.setTexture(*((hero.spriteSheetHero.getVectorSpriteJ(1)).getTexture()));
hero.spriteSheetHero.mainSprite.move(-5, 0);
if(elapse.asMilliseconds() <= 300)
{
hero.spriteSheetHero.mainSprite.setTexture(*(hero.spriteSheetHero.getVectorSpriteJ(4)).getTexture());
}
if(elapse.asMilliseconds() >= 600)
{
hero.spriteSheetHero.mainSprite.setTexture(*(hero.spriteSheetHero.getVectorSpriteJ(5)).getTexture());
clock.restart();
}
}
clock.restart();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
elapse = clock.getElapsedTime();
hero.spriteSheetHero.mainSprite.setTexture(*((hero.spriteSheetHero.getVectorSpriteJ(3)).getTexture()));
hero.spriteSheetHero.mainSprite.move(5, 0);
if(elapse.asMilliseconds() <= 300)
{
hero.spriteSheetHero.mainSprite.setTexture(*(hero.spriteSheetHero.getVectorSpriteJ(10)).getTexture());
}
if(elapse.asMilliseconds() >= 600)
{
hero.spriteSheetHero.mainSprite.setTexture(*(hero.spriteSheetHero.getVectorSpriteJ(11)).getTexture());
clock.restart();
}
}
clock.restart();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
elapse = clock.getElapsedTime();
hero.spriteSheetHero.mainSprite.setTexture(*((hero.spriteSheetHero.getVectorSpriteJ(0)).getTexture()));
hero.spriteSheetHero.mainSprite.move(0, 5);
if(elapse.asMilliseconds() <= 300)
{
hero.spriteSheetHero.mainSprite.setTexture(*(hero.spriteSheetHero.getVectorSpriteJ(8)).getTexture());
}
if(elapse.asMilliseconds() >= 600)
{
hero.spriteSheetHero.mainSprite.setTexture(*(hero.spriteSheetHero.getVectorSpriteJ(9)).getTexture());
clock.restart();
}
}
clock.restart();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
elapse = clock.getElapsedTime();
hero.spriteSheetHero.mainSprite.setTexture(*(((hero.spriteSheetHero.getVectorSpriteJ(2)).getTexture())));
hero.spriteSheetHero.mainSprite.move(0, -5);
if(elapse.asMilliseconds() <= 300)
{
hero.spriteSheetHero.mainSprite.setTexture(*(hero.spriteSheetHero.getVectorSpriteJ(6)).getTexture());
}
if(elapse.asMilliseconds() >= 600)
{
hero.spriteSheetHero.mainSprite.setTexture(*(hero.spriteSheetHero.getVectorSpriteJ(7)).getTexture());
clock.restart();
}
}
clock.restart();
window.draw(hero.spriteSheetHero.mainSprite);
window.display();
}
return 0;
}
void Hero::setHealth(int health)
{
healthH = health;
}
void Spritesheet::setVectorSprite(int vectorX[3], int vectorY[3])
{
sf::Sprite spritetest;
for(int i = 0; i < SIZE; i++)
{
vectorPX[i] = vectorX[i];
vectorPY[i] = vectorY[i];
textureHero[i].loadFromFile("spritesheet.png", sf::IntRect(vectorX[i] ,vectorY[i] ,18 , 26 ));
spritetest.setTexture(textureHero[i]);
getVectorSprite().push_back(spritetest);
}
}
std::vector<sf::Sprite> & Spritesheet::getVectorSprite()
{
return vectorSprite;
}
int & Hero::getlastX()
{
return lastposX;
}
int & Hero::getlastY()
{
return lastposY;
}
void Hero::setPos(int posX, int posY)
{
curX = posX;
curY = posY;
spriteSheetHero.mainSprite.setPosition(curX, curY);
}
sf::Sprite & Spritesheet::getVectorSpriteJ(int j)
{
return vectorSprite[j];
}