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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Confulished

Pages: [1]
1
General / Re: I need some help - Player animation class
« on: September 12, 2016, 04:05:35 pm »
It worked! Thank you!  :D

2
General / 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?

Pages: [1]