SFML community forums

Help => Graphics => Topic started by: kingwill on July 08, 2013, 02:15:20 am

Title: changing texture on keypress
Post by: kingwill on July 08, 2013, 02:15:20 am
hello i was experimenting trying to change the texture of a sprite on keypress but i realized that on keypress nothing happens what could i do to fix this problems



   sf::Texture tex[3];
   if(!tex[0].loadFromFile("data/Meifront.png",sf::IntRect(0,0,60.75f,76.0f))){}
   if(!tex[1].loadFromFile("data/Meifront.png",sf::IntRect(0,0,121.5f,76.0f))){}
   if(!tex[2].loadFromFile("data/Meifront.png",sf::IntRect(0,0,183.0f,76.0f))){}
   spr.setTexture(tex[0]);


void Game::GameLoop()
{

    while(window.isOpen())
    {
        while(window.pollEvent(e))
        {
            switch(e.type)
            {
                case sf::Event::Closed:
                    window.close();

                case sf::Event::KeyPressed :
                    if(e.key.code == sf::Keyboard::Escape)
                    {
                        window.close();
                    }
                    if(e.key.code == sf::Keyboard::Left)
                    {
                        tex[0].update(window, spr.getPosition().x, spr.getPosition().y);
                        spr.setTexture(tex[0]);
                        //Player1->move(-3,0);
                          }
                    if(e.key.code == sf::Keyboard::Right)
                    {
                        spr.setTexture(tex[1]);
                        Player1->move(3,0);
                    }
                    if(e.key.code == sf::Keyboard::Up)
                    {
                                                spr.setTexture(tex[2]);
                        }
                    if(e.key.code == sf::Keyboard::Down)
                    {
                        spr.setTexture(tex[3]);
                        }

                    }//switch
            }//while loop






 
Title: Re: changing texture on keypress
Post by: zsbzsb on July 08, 2013, 03:29:12 am
You need to go back and read the documentation/tutorials.

tex[0].update(window, spr.getPosition().x, spr.getPosition().y);

For starters calling update on a texture while passing a reference to a window causes the texture to copy from the window.

Player1->move(3,0);

Do not do movement inside your event loop, this is wrong. Also when you do movement you need to consider how much time has passed since your game will not run at the same speed on every computer.

if(!tex[0].loadFromFile("data/Meifront.png",sf::IntRect(0,0,60.75f,76.0f))){}
   if(!tex[1].loadFromFile("data/Meifront.png",sf::IntRect(0,0,121.5f,76.0f))){}
   if(!tex[2].loadFromFile("data/Meifront.png",sf::IntRect(0,0,183.0f,76.0f))){}

Without seeing your png file it looks also as if you do not understand the way sf::IntRect handles coordinates. sf::IntRect() takes the position and the size of an rectangle. The way you are doing it now you are going from a small rectangle to a larger rectangle while covering the same area as before.

Texture Documentation (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Texture.php)
Rect Documentation (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Rect.php)