Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: moving a character through a class and a spritesheet class  (Read 8500 times)

0 Members and 1 Guest are viewing this topic.

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
moving a character through a class and a spritesheet class
« on: August 14, 2013, 02:19:52 pm »
Hello
I just started SFML and wanted to write a program that moves a ball.

here is the code:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

sf::Event event;

class Ball
{
    int CircleR, Redd, Greenn, Bluee;
    sf::CircleShape shape;

    public:

    Ball(int CircleR,int Redd,int Greenn,int Bluee);
    Ball();
    sf::CircleShape getCircle();
    void moveCircle(int x, int y);

};

int main()
{
    Ball ball(100.f, 250, 0, 0);

    sf::RenderWindow window(sf::VideoMode(800,600), "SFML Test");
    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                window.close();
                break;

                default:
                break;
            }

        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            ball.moveCircle(-50,0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            ball.moveCircle(50,0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            ball.moveCircle(0,50);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            ball.moveCircle(0,-50);
        }
        window.clear(sf::Color::Black);

        window.draw(ball.getCircle());

        window.display();
    }
    return 0;

}


Ball::Ball(int CircleR, int Redd, int Greenn, int Bluee)
{
    CircleR = CircleR;
    Redd = Redd;
    Greenn = Greenn;
    Bluee = Bluee;

    shape.setRadius(CircleR);
    shape.setFillColor(sf::Color(Redd,Greenn,Bluee));
    shape.setPosition(100,100);
}

Ball::Ball()
{
    shape.setRadius(50);
    shape.setFillColor(sf::Color(100,100,100));
    shape.setPosition(100,100);
}

sf::CircleShape Ball::getCircle()
{
    return shape;
}

void Ball::moveCircle(int x, int y)
{
    getCircle().move(x, y);
}
 

When I press left/up/down/right nothing happens. Some insight? Thanks
« Last Edit: August 15, 2013, 12:23:14 am by Engineer »

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: moving a circle through a class
« Reply #1 on: August 14, 2013, 02:39:29 pm »
Quote
sf::CircleShape Ball::getCircle()
{
    return shape;
}

void Ball::moveCircle(int x, int y)
{
    getCircle().move(x, y);
}
It's not SFML related but C++ related.
getCircle() returns a copy of shape, and moveCircle calls move() on the copy. It essentially does nothing.
Either directly use shape in your moveCircle method, or make getCircle return a reference.

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: moving a circle through a class
« Reply #2 on: August 14, 2013, 02:52:36 pm »
Thanks. But even if I use shape, it will disappear. And I tried to lower down the move(50,0) to move(10,0).
Since I really need to make the getcircle function to work (otherwise it will draw a copy of ball), can you tell me how I can return an address? I thought I would have needed to use pointers but I really don't know how I could do that. I want to do it with pointers that are more familiar with me. I recognize its my first time trying to use libraries and classes altogether, I already did functions that worked on pointers but never functions that returns pointers. For example how can I call the draw function with the pointer ?

Here is my new code:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

sf::Event event;

class Ball
{
    int CircleR, Redd, Greenn, Bluee;
    sf::CircleShape * shape;

    public:

    Ball(int CircleR,int Redd,int Greenn,int Bluee);
    Ball();
    sf::CircleShape * getCircle();
    void moveCircle(int x, int y);

};

int main()
{
    Ball ball(100.f, 250, 0, 0);

    sf::RenderWindow window(sf::VideoMode(800,600), "SFML Test");
    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                window.close();
                break;

                default:
                break;
            }

        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            ball.moveCircle(-10,0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            ball.moveCircle(10,0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            ball.moveCircle(0,10);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            ball.moveCircle(0,-10);
        }
        window.clear(sf::Color::Black);

        window.draw(&(ball.getCircle()));

        window.display();
    }
    return 0;

}


Ball::Ball(int CircleR, int Redd, int Greenn, int Bluee)
{
    CircleR = CircleR;
    Redd = Redd;
    Greenn = Greenn;
    Bluee = Bluee;

    shape->setRadius(CircleR);
    shape->setFillColor(sf::Color(Redd,Greenn,Bluee));
    shape->setPosition(100,100);
}

Ball::Ball()
{
    shape->setRadius(50);
    shape->setFillColor(sf::Color(100,100,100));
    shape->setPosition(100,100);
}

sf::CircleShape * Ball::getCircle()
{
    return shape;
}

void Ball::moveCircle(int x, int y)
{
    getCircle()->move(x, y);
}
 

Also forget my last code for the key events, I just wanted to replace the if with a switch statement but it doesn't look like you can replace it with a switch. Thanks for your help.
« Last Edit: August 14, 2013, 02:57:57 pm by Engineer »

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: moving a circle through a class
« Reply #3 on: August 14, 2013, 03:00:13 pm »
Try to avoid pointers as long as they aren't needed.
You should really learn how to use references, pointers or copies before toying with SFML, it's mandatory basic C++ knowledge.

To return a reference in your former code (not in your new one with pointers) you only have to change one thing:
sf::CircleShape& Ball::getCircle()
{
    return shape;
}

Oh and the shape disappears yeah, you're moving it by 50 pixels (now 10) per frame, it's a lot! There is a good chance that if you press a key for a little time the shape will move out of the screen so fast that you won't even see it.
« Last Edit: August 14, 2013, 03:05:20 pm by G. »

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: moving a circle through a class
« Reply #4 on: August 14, 2013, 03:09:35 pm »
Thanks a lot. I'll try to practice a few times more with references and pointers.
But just to know, for the pointer version, I would need to put CircleShape * shape, name the function getcircle to * getcircle and give &(ball.getCircle()) to .draw() and put getCircle()-> move(x,y) in the last function of my code (Ball::moveCircle) ? Or I miss something?

EDIT: and use "->" in the constructors of Ball. Only thing I do not understand is how to give the shape to draw. :/
Thanks a lot again.
« Last Edit: August 14, 2013, 03:12:54 pm by Engineer »

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: moving a circle through a class
« Reply #5 on: August 14, 2013, 03:17:19 pm »
If I understood correctly, no.
In your code with the pointer, getCircle() return a pointer to an sf::CircleShape. You need to give the CircleShape pointed by your pointer to window.draw().
You need to dereference your pointer and the syntax would be:
window.draw(*(ball.getCircle()));

You also never dynamically allocate memory to your sf::CircleShape pointer. (use new)
« Last Edit: August 14, 2013, 03:21:25 pm by G. »

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: moving a circle through a class
« Reply #6 on: August 14, 2013, 03:24:04 pm »
Yeah exactly what I needed thanks. I admit references look to be simpler to use but from what I understood you can't change references, reference to NULL and must initialize references when you create them, thats why I wanted to use pointers that are more flexible.
The draw function is good but I don't know in the getCircle if I return *shape or shape;
actual code:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

sf::Event event;

class Ball
{
    int CircleR, Redd, Greenn, Bluee;
    sf::CircleShape * shape;

    public:

    Ball(int CircleR,int Redd,int Greenn,int Bluee);
    Ball();
    sf::CircleShape * getCircle();
    void moveCircle(int x, int y);

};

int main()
{
    Ball ball(30, 250, 0, 0);

    sf::RenderWindow window(sf::VideoMode(800,600), "SFML Test");
    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                window.close();
                break;

                default:
                break;
            }

        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            ball.moveCircle(-1,0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            ball.moveCircle(1,0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            ball.moveCircle(0,1);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            ball.moveCircle(0,-1);
        }
        window.clear(sf::Color::Black);

        window.draw(*(ball.getCircle()));

        window.display();
    }
    return 0;

}


Ball::Ball(int CircleR, int Redd, int Greenn, int Bluee)
{
    CircleR = CircleR;
    Redd = Redd;
    Greenn = Greenn;
    Bluee = Bluee;

    shape->setRadius(CircleR);
    shape->setFillColor(sf::Color(Redd,Greenn,Bluee));
    shape->setPosition(100,100);
}

Ball::Ball()
{
    shape->setRadius(50);
    shape->setFillColor(sf::Color(100,100,100));
    shape->setPosition(100,100);
}

sf::CircleShape * Ball::getCircle()
{
    return shape;
}

void Ball::moveCircle(int x, int y)
{
    getCircle()->move(x, y);
}
 

Thanks you a billion times and sorry for abusing of your kindness :p
EDIT: it compiles but crashes.
« Last Edit: August 14, 2013, 03:29:26 pm by Engineer »

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: moving a circle through a class
« Reply #7 on: August 14, 2013, 03:41:47 pm »
Your pointer is uninitialized, it points to an unknown location (could be anywhere) and you're using it nevertheless.
You need to initialize it, with new.
shape = new sf::CircleShape;

If shape is an sf::CircleShape* and you want getCircle() to return an sf::CircleShape*, then you just have to
return shape;

Everything will be clearer when you learn/understand it. :p

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: moving a circle through a class
« Reply #8 on: August 14, 2013, 03:53:52 pm »
Yeah true I always forget about initializing pointers. F***. Thanks a lot. :)
I come from C where I was using malloc() and free(), I suppose that in C++ the object is deleted in the end of the scope of the file.
Thanks a lot again. :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: moving a circle through a class
« Reply #9 on: August 14, 2013, 04:18:10 pm »
I suppose that in C++ the object is deleted in the end of the scope of the file.
No, it's not. If you use new, you have to clean up with delete. Same for new[] and delete[].

But in C++ you usually don't do that, memory is managed automatically by means of the RAII idiom. Instead of sf::CircleShape*, you simply use sf::CircleShape. If you ever need pointers (e.g. for polymorphism), you replace sf::Shape* with std::unique_ptr<sf::Shape> which automatically invokes delete for you.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: moving a circle through a class
« Reply #10 on: August 14, 2013, 04:45:53 pm »
Okay thanks a lot. By the way is it normal that when I press left/right/down/up the circle move more than one pixel (I put 1 in each move) ? It moves way too fast :/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: moving a circle through a class
« Reply #11 on: August 14, 2013, 05:31:27 pm »
Yes, since you have a loop that is continuously iterated. For the beginning, you can set a frame rate limit (sf::RenderWindow::setFramerateLimit()), later you can use a more elaborated technique for frame-independent movement.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: moving a circle through a class
« Reply #12 on: August 14, 2013, 06:27:47 pm »
Thanks a lot. By the way I made a new program that got no error and crash :
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <string>
#include <vector>

#define SIZE 3

sf::Event event;
bool failtoOpenFile = false;

class Spritesheet
{
    public:
    sf::Sprite mainSprite;
    sf::Texture textureHero[SIZE];
    int vectorPX[SIZE], vectorPY[SIZE];
    void setVectorSprite(int vectorX[SIZE], int vectorY[SIZE]);
    void setFileName(std::string filename);
    std::vector<sf::Sprite> & getVectorSprite();

    private:
    std::string filenameS;
    std::vector<sf::Sprite> vectorSprite;


};




class Hero
{
public:
    Spritesheet spriteSheetHero;
    void setHealth(int health);
    int lastposX, lastposY;
private:
    int health;
    int curX, curY;


};



int main()
{


    sf::RenderWindow window(sf::VideoMode(800,600), "SFML Char test");
    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                window.close();
                break;

                default:
                break;
            }

        }
        // setting the hero
        Hero hero;
        hero.setHealth(100);
        hero.spriteSheetHero.setFileName("spritesheet.png");
        int testX[3], testY[3];
        testX[0] = 14;
        testX[1] = 49;
        testX[2] = 78;
        testY[0] = 9;
        testY[1] = 9;
        testY[2] = 9;
        hero.spriteSheetHero.setVectorSprite(testX, testY );
        hero.spriteSheetHero.mainSprite = hero.spriteSheetHero.getVectorSprite()[2];

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            hero.spriteSheetHero.mainSprite = hero.spriteSheetHero.getVectorSprite()[1];
            hero.spriteSheetHero.mainSprite.move(-1, 0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            hero.spriteSheetHero.mainSprite = hero.spriteSheetHero.getVectorSprite()[1];
            hero.spriteSheetHero.mainSprite.rotate(180);
            hero.spriteSheetHero.mainSprite.move(1, 0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            hero.spriteSheetHero.mainSprite = hero.spriteSheetHero.getVectorSprite()[0];
            hero.spriteSheetHero.mainSprite.move(0, -1);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            hero.spriteSheetHero.mainSprite = hero.spriteSheetHero.getVectorSprite()[2];
            hero.spriteSheetHero.mainSprite.move(0, 1);
        }
        window.clear(sf::Color::Blue);

        window.draw(hero.spriteSheetHero.mainSprite);

        window.display();
    }
    return 0;

}

void Hero::setHealth(int health)
{
    health = health;
}

void Spritesheet::setVectorSprite(int vectorX[3], int vectorY[3])
{
    for(int i = 0; i < SIZE; i++)
    {
        vectorPX[i] = vectorX[i];
        vectorPY[i] = vectorY[i];
        if(!(textureHero[i].loadFromFile("spritesheet.png", sf::IntRect(vectorPX[i], vectorPY[i], 18, 26))))
        {
            failtoOpenFile = true;
        }
        else
        {
            textureHero[i].loadFromFile("spritesheet.png", sf::IntRect( vectorPX[i],vectorPY[i] , 18,26 ));
            getVectorSprite()[i].setTexture(textureHero[i]);
        }
    }


}
void Spritesheet::setFileName(std::string filename)
{
    filenameS = filename;
}

std::vector<sf::Sprite> & Spritesheet::getVectorSprite()
{
    return vectorSprite;
}
 

Maybe it comes from the fact that I use arrays to initialize the sprite, but can somebody confirm me this?
Thanks a lot. :)

BTW, the file and all the .dll are next to the .exe in the same directory and I'm TOTALLY SURE it's the working directory (did programs to see where files goes when I create them using filestreams before). :)
« Last Edit: August 14, 2013, 06:30:28 pm by Engineer »

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: moving a hero through a class and a spritesheet class
« Reply #13 on: August 14, 2013, 07:56:19 pm »
Now I updated my code with better functions on things that could have possibly gone wrong (aka, get a vector<sf::Sprite> from a class, now I return the <sf::Sprite> I need from the vector).
EDIT: It indeeds come from there, because if I remove my functions and load the sprite with mainsprite it works and render correctly my sprite so it must come from the arrays.
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <string>
#include <vector>

#define SIZE 3

sf::Event event;

class Spritesheet
{
    public:
    sf::Sprite mainSprite;
    sf::Texture textureHero[SIZE];
    int vectorPX[SIZE], vectorPY[SIZE];
    void setVectorSprite(int vectorX[SIZE], int vectorY[SIZE]);
    void setFileName(std::string filename);
    std::vector<sf::Sprite> & getVectorSprite();
    sf::Sprite & getVectorSpriteJ(int j);

    private:
    std::string filenameS;
    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 health;
    int lastposX, lastposY;
    int curX, curY;


};



int main()
{


    sf::RenderWindow window(sf::VideoMode(800,600), "SFML Char test");
    // window.setFramerateLimit(10);
    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                window.close();
                break;

                default:
                break;
            }

        }
        // setting the hero
        Hero hero;
        hero.setHealth(100);
        hero.spriteSheetHero.setFileName("spritesheet.png");
        int testX[3], testY[3];
        testX[0] = 14;
        testX[1] = 49;
        testX[2] = 78;
        testY[0] = 9;
        testY[1] = 9;
        testY[2] = 9;
        hero.spriteSheetHero.setVectorSprite(testX, testY );
        hero.spriteSheetHero.mainSprite = hero.spriteSheetHero.getVectorSpriteJ(2);

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            hero.spriteSheetHero.mainSprite = hero.spriteSheetHero.getVectorSpriteJ(1);
            hero.spriteSheetHero.mainSprite.move(-1, 0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            hero.spriteSheetHero.mainSprite = hero.spriteSheetHero.getVectorSpriteJ(1);
            hero.spriteSheetHero.mainSprite.rotate(180);
            hero.spriteSheetHero.mainSprite.move(1, 0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            hero.spriteSheetHero.mainSprite = hero.spriteSheetHero.getVectorSpriteJ(0);
            hero.spriteSheetHero.mainSprite.move(0, -1);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            hero.spriteSheetHero.mainSprite = hero.spriteSheetHero.getVectorSpriteJ(2);
            hero.spriteSheetHero.mainSprite.move(0, 1);
        }
        window.clear(sf::Color::Blue);

        window.draw(hero.spriteSheetHero.mainSprite);

        window.display();
    }
    return 0;

}

void Hero::setHealth(int health)
{
    health = health;
}

void Spritesheet::setVectorSprite(int vectorX[3], int vectorY[3])
{
    for(int i = 0; i < SIZE; i++)
    {
        vectorPX[i] = vectorX[i];
        vectorPY[i] = vectorY[i];
        textureHero[i].loadFromFile("spritesheet.png", sf::IntRect( vectorPX[i],vectorPY[i] , 18,26 ));
        getVectorSpriteJ(i).setTexture(textureHero[i]);
    }
}

void Spritesheet::setFileName(std::string filename)
{
    filenameS = filename;
}

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];
}
 
« Last Edit: August 14, 2013, 08:06:38 pm by Engineer »

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: moving a hero through a class and a spritesheet class
« Reply #14 on: August 14, 2013, 11:29:20 pm »
Just discovered I was dumb and couldn't simply do sf::that sprite = sf::thatothersprite...
Here is another code that takes care of that (don't mind the comments its for one animation system) :
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <string>
#include <vector>

#define SIZE 3

sf::Event event;

class Spritesheet
{
    public:
    sf::Sprite mainSprite;
    sf::Texture textureHero[SIZE];
    int vectorPX[SIZE], vectorPY[SIZE];
    void setVectorSprite(int vectorX[SIZE], int vectorY[SIZE]);
    void setFileName(std::string filename);
    std::string & getFileName();
    std::vector<sf::Sprite> & getVectorSprite();
    sf::Sprite & getVectorSpriteJ(int j);

    private:
    std::string filenameS;
    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::RenderWindow window(sf::VideoMode(800,600), "SFML Char test");
    // window.setFramerateLimit(10);
    while(window.isOpen())
    {
        sf::Clock clock;
        sf::Time elapse = clock.getElapsedTime();
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                window.close();
                break;

                default:
                break;
            }

        }
        window.clear(sf::Color::Blue);
        // setting the hero
        Hero hero;
        hero.setHealth(100);
        hero.spriteSheetHero.setFileName("spritesheet.png");
        int testX[3] = { 14, 49, 78 };
        int testY[3] = { 9, 9, 9 };
        hero.spriteSheetHero.setVectorSprite(testX, testY );
        hero.spriteSheetHero.mainSprite.setTexture(*((hero.spriteSheetHero.getVectorSpriteJ(2)).getTexture()));

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            clock.restart();
            hero.spriteSheetHero.mainSprite.setTexture(*((hero.spriteSheetHero.getVectorSpriteJ(1)).getTexture()));
            hero.spriteSheetHero.mainSprite.move(-1, 0);
            /*if(elapse == 300)
            {
                hero.spriteSheetHero.mainSprite.setTexture((hero.spriteSheetHero.getVectorSpriteJ()).getTexture());
            }
            if(elapse == 600)
            {
                hero.spriteSheetHero.mainSprite.setTexture((hero.spriteSheetHero.getVectorSpriteJ()).getTexture());
            }
            clock.restart();*/

        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            clock.restart();
            hero.spriteSheetHero.mainSprite.setTexture(*((hero.spriteSheetHero.getVectorSpriteJ(1)).getTexture()));
            hero.spriteSheetHero.mainSprite.rotate(180);
            hero.spriteSheetHero.mainSprite.move(1, 0);
            /*if(elapse == 300)
            {
                hero.spriteSheetHero.mainSprite.setTexture((hero.spriteSheetHero.getVectorSpriteJ()).getTexture());
            }
            if(elapse == 600)
            {
                hero.spriteSheetHero.mainSprite.setTexture((hero.spriteSheetHero.getVectorSpriteJ()).getTexture());
            }
            clock.restart();*/

        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            clock.restart();
            hero.spriteSheetHero.mainSprite.setTexture(*((hero.spriteSheetHero.getVectorSpriteJ(0)).getTexture()));
            hero.spriteSheetHero.mainSprite.move(0, -1);
            /*if(elapse == 300)
            {
                hero.spriteSheetHero.mainSprite.setTexture((hero.spriteSheetHero.getVectorSpriteJ()).getTexture());
            }
            if(elapse == 600)
            {
                hero.spriteSheetHero.mainSprite.setTexture((hero.spriteSheetHero.getVectorSpriteJ()).getTexture());
            }
            clock.restart();*/

        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            clock.restart();
            hero.spriteSheetHero.mainSprite.setTexture(*(((hero.spriteSheetHero.getVectorSpriteJ(2)).getTexture())));
            hero.spriteSheetHero.mainSprite.move(0, 1);
            /*if(elapse == 300)
            {
                hero.spriteSheetHero.mainSprite.setTexture((hero.spriteSheetHero.getVectorSpriteJ()).getTexture());
            }
            if(elapse == 600)
            {
                hero.spriteSheetHero.mainSprite.setTexture((hero.spriteSheetHero.getVectorSpriteJ()).getTexture());
            }
            clock.restart();*/

        }
        hero.setPos(100,100);
        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])
{
    for(int i = 0; i < SIZE; i++)
    {
        vectorPX[i] = vectorX[i];
        vectorPY[i] = vectorY[i];
        textureHero[i].loadFromFile(getFileName(), sf::IntRect(vectorPX[i] ,vectorPY[i] ,18 ,26 ));
        getVectorSpriteJ(i).setTexture(textureHero[i]);
    }
}

void Spritesheet::setFileName(std::string filename)
{
    filenameS = filename;
}

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];
}
std::string & Spritesheet::getFileName()
{
    return filenameS;
}