When my sprite is animating the window will flicker black, as if one frame it decides to clear the screen and then forgets to draw before displaying.
I made a very short youtube video of the problem.
Here is the main event loop
while(Run==true)
{
while(window.pollEvent(event))
{
if(NewGame==true)
{
Quit=Splash(window,desktop,NewGame,text);
if(Quit==true)
{
return 0;
}
}
NZ=clock.getElapsedTime().asSeconds();
Next=false;
if(event.type==sf::Event::Closed)
{
return 0;
}
else if((event.type==sf::Event::KeyPressed)&&(event.key.code==sf::Keyboard::Escape))
{
return 0;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
Player1.MR=true;
Player1.Id=false;
if (NZ>0.05f)
{
Next=true;
Player1.Animate_Self(Next);
clock.restart();
}
}
else if((event.type==sf::Event::KeyReleased)&&(event.key.code==sf::Keyboard::D))
{
Player1.MR=false;
Player1.Id=true;
Player1.Animate_Self(Next);
clock.restart();
}
Display_Main(window,Player1);
}
}
Here is the relevant animation code
void Entity::Display_Self(sf::RenderWindow& window)
{
sprite.setPosition(sf::Vector2f(X,Y));
window.draw(sprite);
}
void Entity::Animate_Self(bool& Next)
{
if(MR==true)
{
if(Next==true)
{
XR+=64;
if(XR>256)
{
XR=64;
}
}
}
else if(Id==true)
{
XR=0;
}
sprite.setTextureRect(sf::IntRect(XR,YR,64,64));
}
Here is my call to display everything
void Display_Main(sf::RenderWindow& window,Player& Player1)
{
window.clear(sf::Color(0,0,0,255));
Player1.Display_Self(window);
window.display();
}