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

Author Topic: setTextureRect() not changing the sprite  (Read 1348 times)

0 Members and 1 Guest are viewing this topic.

MrSnappingTurtle

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
setTextureRect() not changing the sprite
« on: May 30, 2014, 07:42:02 pm »
Hi,

So in my header file for the player I have this code:

const std::array<sf::IntRect, 3> INTERACT
        {{
                sf::IntRect(11, 0, 11, 10),
                sf::IntRect(22, 0, 11, 10),
                sf::IntRect(11, 0, 11, 10)
        }};

And then in the source file I have this inside the update function:
 if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && animationState != interact)
        {
                 animationState = interact;

                 for (int i = 0; i < INTERACT.size(); i++)
                 {
                         sf::Clock frameTimer;
                         sprite.setTextureRect(INTERACT[i]);
                         std::cout << "Sprite: " << i << "should have changed..." << std::endl;
                         //Waits to iterate the next frame
                         while (frameTimer.getElapsedTime().asMilliseconds() < 100) {}
                 }

                 animationState = idle;
                 std::cout << "done!";
        }

Now, the problem is that the sprite.setTextureRect(INTERACT(i)) part does not work. The cout does run so I know that the function is being called. If I hold down the spacebar, however, it will eventually be showing just the last frame. Is there something obvious that I'm missing?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: setTextureRect() not changing the sprite
« Reply #1 on: May 30, 2014, 07:56:16 pm »
You aren't drawing the sprite anywhere in that function, so it just cycles through the texture rects without putting anything on screen.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: setTextureRect() not changing the sprite
« Reply #2 on: May 31, 2014, 09:48:05 am »
Not directly related to you problem, but don't use while (frameTimer.getElapsedTime().asMilliseconds() < 100) {}. Instead use sf::sleep. This way, instead of having a process using 100% of the CPU for nothing, others processes can do stuff.
SFML / OS X developer