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

Author Topic: How animation with touch button  (Read 1504 times)

0 Members and 1 Guest are viewing this topic.

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
How animation with touch button
« on: September 22, 2022, 09:40:43 pm »
Hello,
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 still plays automaticly one time when i touched the Button, without hold the finger on and without repeating the animation, how can i fix it? Here is the part if the code:


...

//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();
                        }                              
                }                      
 


smurf

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: How animation with touch button
« Reply #1 on: September 26, 2022, 04:45:51 pm »
Hi, it seems like you have something like this:

while (true)
    if (touching button)
        run animation()
 


but you probably want something like this:

while (true)
    if (touching button and isShouldRunAnimation == false)
        isShouldRunAnimation = true
        reset animation to beginning
    if (isShouldRunAnimation)
        run animation()
        if (animation is done)
            isShouldRunAnimation = false
 


« Last Edit: September 26, 2022, 04:47:37 pm by smurf »

 

anything