Hi guys! i'm very new to C++ and trying to set up a walk animation but when i run the program my animation is way to fast.
I'm currently following a tutorial how to make an animation, but i dont know how to alter the speed. The maker of the tutorial is not responding to my questions so thats why i'm here
Can you guys help me find the right alteration to my code to make my animation slow down?
My code looks like this:
main.ccp:int main()
{
RenderWindow window(VideoMode(600, 400), "first window made in C++", Style::Close | Style::Titlebar | Style::Resize);
RectangleShape player1(Vector2f(50.0f, 50.0f));
player1.setPosition(150.0f, 300.0f);
Texture playerTexture;
playerTexture.loadFromFile("baldricfrontwalksheet_01.png");
player1.setTexture(&playerTexture);
Animation animation(&playerTexture, Vector2u(9, 4), 0.3f);
float deltaTime = 0.0f;
Clock clock;
while(window.isOpen())
{
Event evnt;
while (window.pollEvent(evnt))
{
deltaTime = clock.restart().asSeconds();
switch (evnt.type)
{
case Event::Closed:
window.close();
break;
case Event::Resized:
printf("New window width: %i New window height %i", evnt.size.width, evnt.size.height);
break;
// Input possible
case Event::TextEntered:
if (evnt.text.unicode < 128)
{
printf("%c", evnt.text.unicode);
}
}
if (evnt.type == evnt.Closed)
{
window.close();
}
}
/* Player1 movement */
if (Keyboard::isKeyPressed(Keyboard::Key::A))
{
player1.move(-0.1f, 0.0f);
}
if (Keyboard::isKeyPressed(Keyboard::Key::D))
{
player1.move(0.1f, 0.0f);
}
if (Keyboard::isKeyPressed(Keyboard::Key::W))
{
player1.move(0.0f, -0.1f);
}
if (Keyboard::isKeyPressed(Keyboard::Key::S))
{
player1.move(0.0f, 0.1f);
}
/*
if (Mouse::isButtonPressed(Mouse::Left))
{
Vector2i mousePos = Mouse::getPosition(window);
player.setPosition(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
}
*/
animation.Update(3, deltaTime);
player1.setTextureRect(animation.uvRect);
window.clear();
window.draw(player1);
/*window.draw(player2);*/
window.display();
}
return 0;
}
animation.ccpAnimation::Animation(Texture* texture, Vector2u imageCount, float switchTime)
{
this->imageCount = imageCount;
this->switchTime = switchTime;
totalTime = 0.0f;
currentImage.x = 0;
uvRect.width = int(texture->getSize().x / float(imageCount.x));
uvRect.height = int(texture->getSize().y / float(imageCount.y));
}
Animation::~Animation()
{
}
void Animation::Update(int row, float deltaTime)
{
currentImage.y = row;
totalTime += deltaTime;
if (totalTime >= switchTime)
{
totalTime -= switchTime;
currentImage.x++;
if (currentImage.x >= imageCount.x)
{
currentImage.x = 0;
}
}
uvRect.left = currentImage.x * uvRect.width;
uvRect.top = currentImage.y * uvRect.height;
}
Animation.h:class Animation
{
public:
Animation(Texture* texture, Vector2u imageCount, float switchTime);
~Animation();
void Update(int row, float deltaTime);
public:
IntRect uvRect;
private:
Vector2u imageCount;
Vector2u currentImage;
float totalTime;
float switchTime;
};
Greetings,
Olivier