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

Author Topic: [Q] Proper way to slow down a Sprite during movement?  (Read 3911 times)

0 Members and 1 Guest are viewing this topic.

UchihaKite

  • Newbie
  • *
  • Posts: 33
    • View Profile
    • Email
[Q] Proper way to slow down a Sprite during movement?
« on: May 08, 2016, 12:03:59 am »
Good Afternoon,
I have experience in C++, however, I am very new with SFML. I managed to get a sprite on the screen, with the ability to not only move, but to go through the proper animations well in movement. My issue though, is the sprite itself isn't move too fast, but the animation of his movement is moving way to quickly. If that makes sense. Provided below is my code. I apologize if I put this up incorrectly, first time posting on here. Normally I am able to figure things out on my own, but I cannot seem to figure this one out.

#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

int main()
{

        int rLinkX = 0;
        int rLinkY = 3;
        int rIdle = 0;

        int lLinkX = 0;
        int lLinkY = 2;
        int lIdle = 0;

        int upLinkX = 0;
        int upLinkY = 1;
        int upIdle = 0;

        int dwnLinkX = 0;
        int dwnLinkY = 0;
        int dwnIdle = 0;

        sf::RenderWindow window(sf::VideoMode(1024, 512), "Welcome to SDVA 203!");

        sf::CircleShape shape(75.f);
        shape.setPosition(200, 200);

        window.setMouseCursorVisible(false);

        sf::Texture texture;
        if (!texture.loadFromFile("Content/Crosshair.png"))
        {
                cout << "Could not find image." << endl; // error...
        }
        shape.setTexture(&texture);

        sf::Sprite sprite;
       
        sf::Texture linkTexture;
        if (!linkTexture.loadFromFile("Content/zelda.png", sf::IntRect(0 * 40, 0 * 40, 40, 40)))
        {
                cout << "Could not find image." << endl; // error...
        }
        sprite.setTexture(linkTexture);

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
               
                        if (event.type == sf::Event::KeyPressed)
                        {
                                if (event.key.code == sf::Keyboard::Escape)
                                {
                                        window.close();
                                }

                                if (event.key.code == sf::Keyboard::Left)
                                {
                                        lIdle = 0;
                                        rIdle = 0;
                                        upIdle = 0;
                                        dwnIdle = 0;
                                        cout << "The Left Arrow Key Was Pressed" << endl;
                                        sprite.setTextureRect(sf::IntRect(lLinkX * 40, lLinkY * 40, 40, 40));
                                        if (!linkTexture.loadFromFile("Content/zelda.png"))
                                        {
                                                // error...
                                        }
                                        sprite.setTexture(linkTexture);
                                        sprite.move(-5, 0);
                                }
                                if (event.key.code == sf::Keyboard::Right)
                                {
                                        lIdle = 0;
                                        rIdle = 0;
                                        upIdle = 0;
                                        dwnIdle = 0;
                                        cout << "The Right Arrow Key Was Pressed" << endl;
                                        sprite.setTextureRect(sf::IntRect(rLinkX * 40, rLinkY * 40, 40, 40));
                                        if (!linkTexture.loadFromFile("Content/zelda.png"))
                                        {
                                                // error...
                                        }
                                        sprite.setTexture(linkTexture);
                                        sprite.move(5, 0);
                                }
                                if (event.key.code == sf::Keyboard::Up)
                                {
                                        lIdle = 0;
                                        rIdle = 0;
                                        upIdle = 0;
                                        dwnIdle = 0;
                                        cout << "The Up Arrow Key Was Pressed" << endl;
                                        sprite.setTextureRect(sf::IntRect(upLinkX * 40, upLinkY * 40, 40, 40));
                                        if (!linkTexture.loadFromFile("Content/zelda.png"))
                                        {
                                                // error...
                                        }
                                        sprite.setTexture(linkTexture);
                                        sprite.move(0, -5);
                                }
                                if (event.key.code == sf::Keyboard::Down)
                                {
                                        lIdle = 0;
                                        rIdle = 0;
                                        upIdle = 0;
                                        dwnIdle = 0;
                                        cout << "The Down Arrow Key Was Pressed" << endl;
                                        sprite.setTextureRect(sf::IntRect(dwnLinkX * 40, dwnLinkY * 40, 40, 40));
                                        if (!linkTexture.loadFromFile("Content/zelda.png"))
                                        {
                                                // error...
                                        }
                                        sprite.setTexture(linkTexture);
                                        sprite.move(0, 5);
                                }

                        }

                        if (event.type == sf::Event::KeyReleased)
                        {
                                if (event.key.code == sf::Keyboard::Right)
                                {
                                        rIdle = 1;
                                }

                                if (event.key.code == sf::Keyboard::Left)
                                {
                                        lIdle = 1;
                                }

                                if (event.key.code == sf::Keyboard::Up)
                                {
                                        upIdle = 1;
                                }

                                if (event.key.code == sf::Keyboard::Down)
                                {
                                        dwnIdle = 1;
                                }
                        }

                                if (event.type == sf::Event::MouseButtonPressed)
                                {
                                        if (event.mouseButton.button == sf::Mouse::Right)
                                        {
                                                std::cout << "the right button was pressed" << std::endl;
                                               
                                        }
                                }
                               

                                if (event.type == sf::Event::MouseWheelScrolled)
                                {
                                        if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
                                                std::cout << "wheel type: vertical" << std::endl;
                                        std::cout << "wheel movement: " << event.mouseWheelScroll.delta << std::endl;
                                        std::cout << "mouse x: " << event.mouseWheelScroll.x << std::endl;
                                        std::cout << "mouse y: " << event.mouseWheelScroll.y << std::endl;

                                        if (event.mouseWheelScroll.delta == 1)
                                        {
                                                shape.scale(1.8, 1.8);
                                        }
                                        else if (event.mouseWheelScroll.delta == -1)
                                        {
                                                shape.scale(.8, .8);
                                        }
                                }
                }


                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        cout << "Left Arrow" << endl;

                                lLinkX++;
                                if (lLinkX >= 7)
                                {
                                        lLinkX = 0;
                                }
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        cout << "Right Arrow" << endl;
                        rLinkX++;
                        if (rLinkX >= 7)
                        {
                                rLinkX = 0;
                        }
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        cout << "Left Arrow" << endl;
                        dwnLinkX++;
                        if (dwnLinkX >= 7)
                        {
                                dwnLinkX = 0;
                        }
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        cout << "Right Arrow" << endl;
                        upLinkX++;
                        if (upLinkX >= 7)
                        {
                                upLinkX = 0;
                        }
                }

                if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
                {
                        shape.setPosition(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y);
                }

                if (rIdle == 1)
                {
                        sprite.setTextureRect(sf::IntRect(rLinkX * 40, rLinkY * 40, 40, 40));
                        if (!linkTexture.loadFromFile("Content/zelda.png"))
                        {
                                // error...
                        }
                        sprite.setTexture(linkTexture);
                        rLinkX++;
                        if (rLinkX >= 7)
                        {
                                rLinkX = 0;
                        }
                }

                if (lIdle == 1)
                {
                        sprite.setTextureRect(sf::IntRect(lLinkX * 40, lLinkY * 40, 40, 40));
                        if (!linkTexture.loadFromFile("Content/zelda.png"))
                        {
                                // error...
                        }
                        sprite.setTexture(linkTexture);

                                lLinkX++;
                                if (lLinkX >= 7)
                                {
                                        lLinkX = 0;
                                }

                }

                if (upIdle == 1)
                {
                        sprite.setTextureRect(sf::IntRect(upLinkX * 40, upLinkY * 40, 40, 40));
                        if (!linkTexture.loadFromFile("Content/zelda.png"))
                        {
                                // error...
                        }
                        sprite.setTexture(linkTexture);
                        upLinkX++;
                        if (upLinkX >= 7)
                        {
                                upLinkX = 0;
                        }
                }

                if (dwnIdle == 1)
                {
                        sprite.setTextureRect(sf::IntRect(dwnLinkX * 40, dwnLinkY * 40, 40, 40));
                        if (!linkTexture.loadFromFile("Content/zelda.png"))
                        {
                                // error...
                        }
                        sprite.setTexture(linkTexture);
                        dwnLinkX++;
                        if (dwnLinkX >= 7)
                        {
                                dwnLinkX = 0;
                        }
                }

                shape.setOrigin(75, 75);
                window.clear();
                window.draw(shape);
                window.draw(sprite);
                window.display();

        }

        return 0;
}
 

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: [Q] Proper way to slow down a Sprite during movement?
« Reply #1 on: May 08, 2016, 02:18:54 am »
Hi,

Congratulations on making some things work on your own!

The short answer is that you could look at SFML clock and time objects and start calculating how many microseconds are passing by in each iteration of the game loop and only trigger animation changes after a specific amount of time goes by.  This is a short answer of how you decouple the timing of animation from the timing of your game loop.

The long answer is that your main game loop has several other "design type" issues that need to be fixed soon or at the same time you are fixing the animation problem.

Your main game loop has glued together event handling + animation + loading textures and you'll really want to separate out those things into different functions soon.  You won't want to be loading heavy textures during event handling for example, it  will be very inefficient soon.

Hope this helps.



UchihaKite

  • Newbie
  • *
  • Posts: 33
    • View Profile
    • Email
Re: [Q] Proper way to slow down a Sprite during movement?
« Reply #2 on: May 10, 2016, 08:41:16 pm »
Thank you for the reply! I thought I would get an email notification if I got a reply, so I apologize for the delayed response. I'm still extremely new to all of this, so in terms of your short answer, how would I go about doing that? I have changed my code around, however it is basically the same. The only problems I have now, are when I change directions, the sprite moves, but doesn't start his animation rotation for about a second.

Typically I would use a function to make everything easier, how my teacher told us to do this without the use of functions >.< He provided us a video of his final product, and mine is basically the same, aside from the issue I stated above, and when he moves diagonally the sprite plays the left/right movement animation.

I apologize for my noobness, normally with C++ I am able to figure things out on my own, but he kind of randomly dropped SFML on us and I am a little lost.

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: [Q] Proper way to slow down a Sprite during movement?
« Reply #3 on: May 11, 2016, 04:51:00 am »
Hi,

Ah, it makes more sense now if this is a class project  why the code would look this way.

The delay might be coming from the fact you are repeatedly loading the same texture from the same file during every key press and key release.

You could try loading linktexture ONCE in the entire program before the loop starts and then just setting the texture rectangle within the "if statements" like you are already doing to see if clears the delay.

As a side note, not to ask personal questions, but I'm somewhat curious what kind of awesome school is teaching SFML :)


UchihaKite

  • Newbie
  • *
  • Posts: 33
    • View Profile
    • Email
Re: [Q] Proper way to slow down a Sprite during movement?
« Reply #4 on: May 11, 2016, 10:12:33 pm »
Hello!

Thank you for the reply and the suggestion. Luckily I have a school job, so it gives me a lot of time in front of the computer. I'll be sure to implement your suggestions and let you know how it goes :)

Currently I go to the Art Institute, but my teacher, Keith, thought it would be a good idea to introduce us to SFML, and since he is a programmer for Overwatch I am willing to learn anything he suggests lol

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: [Q] Proper way to slow down a Sprite during movement?
« Reply #5 on: May 12, 2016, 01:43:43 am »
You're welcome, good luck!

UchihaKite

  • Newbie
  • *
  • Posts: 33
    • View Profile
    • Email
Re: [Q] Proper way to slow down a Sprite during movement?
« Reply #6 on: May 16, 2016, 12:52:08 am »
Just wanted to let you know that I managed to complete the assignment! I removed all of my events, excluding the ones relating to the mouse, did a little code cleaning and it worked perfectly! Thank you for your help man :D

 

anything