Sfml how can I stop repeating an animation?

Viewed 40

here i edit my complete Question for better undertstanding with new better Code. So i have a Spritesheet (the animation is in 24 Frames), now it only plays when my finger is on the touch Button (RectangleShape button). But i need to change this, i want that the SpriteSheet animation (24 Frames)still plays automaticly one time when i touched the Button, without hold the finger on, how can i fix it?

    int main(){
        
    sf::RenderWindow window(sf::VideoMode(1200, 2600), "Title");
    
    //init  Sprite( its a SpriteSheet)
    sf::Texture texture;
    texture.loadFromFile("test1.png");
    sf::Sprite sprite;
    sprite.setPosition(300, 500);
    sprite.setScale(5, 5);
    sprite.setTexture(texture);
    
    //init Button
    sf::RectangleShape button;
    button.setSize(sf::Vector2f(600, 200));
    button.setPosition( sf::Vector2f(500, 1900));
   button.setFillColor(sf::Color::Red);
    
    sf::Clock clock;
    sf::Vector2f animRect;
        
        while (window.isOpen())
        {                               
            sf::Event event;
    
    //Set Touch Coordinates to Screen
    sf::Vector2i pixelPos = sf::Touch::getPosition(0, window); 
    sf::Vector2f worldPos = window.mapPixelToCoords(pixelPos);
    
    
    //Set the Rect for the part of the SpriteSheet
    sprite.setTextureRect(sf::IntRect (animRect.x * 101, animRect.y * 232, 101, 232));
            
            while (window.pollEvent(event))
            {                       
                
            if (event.type == sf::Event::Closed)
                    window.close();
                }           
    //FloatRect fot the button Sprite       
    sf::FloatRect btnRect = button.getGlobalBounds();
            
            //if touch the Button
            if(btnRect.contains(worldPos)){
                
                if(clock.getElapsedTime().asSeconds() >= 0.2){
            animRect.x++;
            if(animRect.x * 101 >= texture.getSize().x)
        animRect.x = 0;
           animRect.y = 0;
            clock.restart();
                }               
            }           
                window.clear(sf::Color::Blue);
                window.draw(sprite);
                window.draw(button);
                window.display();
        }
        return 0;
    
    }
0 Answers
Related