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

Author Topic: my animation function is not working  (Read 1901 times)

0 Members and 1 Guest are viewing this topic.

ahmads1990

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
my animation function is not working
« on: March 13, 2020, 08:11:26 pm »
i do have problem with my animation
when going to last fram or picture in sprite sheet the animation goes blank for a sec then starts all over again
i have read about this is cause by the rectangle is bigger than actual texture and can be fixed if i set repeated to true but my texture coordinates are 350 for width and 100 for height
and i do have another problem that the animation  is too slow how can i fix it?
and how can i fix the animation not to go blank after last picture?

here is my code for animation function

Sprite Player::walk()
{
        if (clock.getElapsedTime().asSeconds() > 1.f)
        {
               
                if (source1 == 350)
                        source1 = 50;
       
                else
                        source1 += 50;
                cout << source1<<"\n";
                player.setTextureRect(IntRect (source1, 50 * direction, 50, 50));
                clock.restart();
        }
        return player;
}
 
code for movment
void Player::move()
{
        if (Keyboard::isKeyPressed(Keyboard::W))
                player.move(0.f, -10.f);
        if (Keyboard::isKeyPressed(Keyboard::A))
        {
                player.move(-10.f, 0.f);
                direction = 1;
                walk();
               
        }
        if (Keyboard::isKeyPressed(Keyboard::S))
                player.move(0.f, 10.f);
        if (Keyboard::isKeyPressed(Keyboard::D))
        {
                player.move(10.f, 0.f);
                direction = 0;
                walk();
               
        }
         

}
loading files
Player::Player() :player(texture, source),source1(50)
{
        //load the running animation
        if (!texture.loadFromFile("running4.png"))
        cout << "Error loading player image";
        player.setScale(3, 3);
        direction = 0;
       
}
class player
#pragma once
#include "Map.h"
class Player
{
public:
        Texture texture;
        IntRect source;
        Sprite player;
        Clock clock;
        int direction;
        int source1;

public:
        Player();
        ~Player();
        Sprite walk();
        void move();
        void boundary();
        bool isAnyKeyPressed();
};

 

Fx8qkaoy

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: my animation function is not working
« Reply #1 on: March 14, 2020, 11:10:03 am »
"source1" is never 0, which means the first frame will never be hit, maybe except when u construct ur player

If entering the "walk()" "source1" is 300, it will increase to 350, which will result in a rectangle with left margin 350 and right margin 400, exactly 1 frame after ur spritesheet ended. Use "if(source1 == 300)" rather than "if(source1 == 350)"

U're updating the current frame when 1 sec has elapsed. To make animation faster u need to update the frame faster, like using .5f rather than 1.f