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

Author Topic: I need some help - Player animation class  (Read 1081 times)

0 Members and 1 Guest are viewing this topic.

Confulished

  • Newbie
  • *
  • Posts: 2
    • View Profile
I need some help - Player animation class
« on: September 11, 2016, 10:17:53 pm »
Some time ago I wanted to make a player-animation-class. There were no errors, but in the end there was just a white square...
Here the header with the class:

#include <SFML/Graphics.hpp>

using namespace sf;

class PlayerMove
{
public:
        void Movement(RenderWindow &Window, float frameCounter, float switchFrame, float frameSpeed, int sourceX, int sourceY);
        void Render(RenderWindow &Window);
        void setTexture(String tex);

private:
        Clock clock;
        Texture PlayerTexture;
        Sprite PlayerSprite;
};
 

Here the constructor:

#include "PlayerMovement.h"
#include <SFML/Graphics.hpp>

using namespace sf;

void PlayerMove::setTexture(String tex)
{
        PlayerTexture.setSmooth(false);
        PlayerSprite.setTexture(PlayerTexture);
        PlayerSprite.setScale(3.f, 3.f);
        PlayerSprite.setPosition(100.f, 100.f);
}

void PlayerMove::Movement(RenderWindow &Window, float frameCounter, float switchFrame, float frameSpeed, int sourceX, int sourceY)
{
        if (Keyboard::isKeyPressed(Keyboard::W))
        {
                sourceY = 3;
                PlayerSprite.move(0, -0.5);

                frameCounter += frameSpeed * clock.restart().asSeconds();
                if (frameCounter >= switchFrame)
                {
                        frameCounter = 0;
                        sourceX++;
                        if (sourceX * 32 >= PlayerTexture.getSize().x)
                                sourceX = 0;
                        PlayerSprite.setTextureRect(IntRect(sourceX * 32, sourceY * 32, 32, 32));
                }
        }
        else if (Keyboard::isKeyPressed(Keyboard::A))
        {
                sourceY = 1;
                PlayerSprite.move(-0.5, 0);

                frameCounter += frameSpeed * clock.restart().asSeconds();
                if (frameCounter >= switchFrame)
                {
                        frameCounter = 0;
                        sourceX++;
                        if (sourceX * 32 >= PlayerTexture.getSize().x)
                                sourceX = 0;
                        PlayerSprite.setTextureRect(IntRect(sourceX * 32, sourceY * 32, 32, 32));
                }
        }
        else if (Keyboard::isKeyPressed(Keyboard::D))
        {
                sourceY = 2;
                PlayerSprite.move(0.5, 0);

                frameCounter += frameSpeed * clock.restart().asSeconds();
                if (frameCounter >= switchFrame)
                {
                        frameCounter = 0;
                        sourceX++;
                        if (sourceX * 32 >= PlayerTexture.getSize().x)
                                sourceX = 0;
                        PlayerSprite.setTextureRect(IntRect(sourceX * 32, sourceY * 32, 32, 32));
                }
        }
        else if (Keyboard::isKeyPressed(Keyboard::S))
        {
                sourceY = 0;
                PlayerSprite.move(0, 0.5);

                frameCounter += frameSpeed * clock.restart().asSeconds();
                if (frameCounter >= switchFrame)
                {
                        frameCounter = 0;
                        sourceX++;
                        if (sourceX * 32 >= PlayerTexture.getSize().x)
                                sourceX = 0;
                        PlayerSprite.setTextureRect(IntRect(sourceX * 32, sourceY * 32, 32, 32));
                }

        }
        else
        {
                sourceX = 1;
        }

}

void PlayerMove::Render(RenderWindow &Window)
{
        Window.draw(PlayerSprite);
}
 

Here the main file:

#include "PlayerMovement.h"
#include <SFML/Graphics.hpp>

using namespace sf;

PlayerMove playerOne;

int main()
{
        playerOne.setTexture("textures/testCharAnimated.png");

        RenderWindow window(VideoMode(1280, 720), "Lost In Dimensions");

        while (window.isOpen())
        {
                Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case Event::Closed:
                                window.close();
                                break;

                        }
                }
                window.clear();
                playerOne.Movement(window, 0.f, 100.f, 500.f, 1, 0);
                playerOne.Render(window);
                window.display();
        }
}
 

PS: Is it possible to do this without a class?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: I need some help - Player animation class
« Reply #1 on: September 12, 2016, 02:44:34 pm »
You're not actually loading the image. You pass the filename as a parameter to your setTexture method but never use it.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Confulished

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: I need some help - Player animation class
« Reply #2 on: September 12, 2016, 04:05:35 pm »
It worked! Thank you!  :D