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

Author Topic: Repeating Sprite Animation and Multiple Sprite Animation Issue (No Sprite Sheet)  (Read 2733 times)

0 Members and 1 Guest are viewing this topic.

LaysPotatoChips

  • Newbie
  • *
  • Posts: 1
    • View Profile
Ive been having this same problem for months now and I have no clue what to do. I have a sprite with a lot of images to make up its animation and I was able to code the sprite and its "Down" animation just fine, I was even able to get it to stop animating when the "S" key is no longer pressed. The problem came when I tried to code my other "Up" animation. Now when ever I try to play test my game the sprite will only animate when going "Up" and will use both animations so both his "Down" and "Up" animations play while moving in the same direction. I was hoping that someone could help explain to me what it is that I am doing wrong or just help me fix this issue in general.

Here is the current code:

GameState.h

#pragma once
#include <SFML/Graphics.hpp>
#include "State.h"
#include "GameLoop.h"
#include "Definitions.h"
#include "Player 1.h"
namespace Cobra
{
        class GameState : public State
        {
        public:
                GameState(GameDataRef data);

                void Init();

                void HandleInput();
                void Update(float dt);
                void Draw(float dt);

        private:
                GameDataRef _data;

                sf::Sprite _background;
                sf::Sprite _Player1;

                Player1 *player1;

                int gameState;
        };
}

GameState.cpp

#include <iostream>
#include <sstream>
#include "GameState.h"
#include "Definitions.h"
namespace Cobra
{
        GameState::GameState(GameDataRef data) : _data(data)
        {

        }

        void GameState::Init()
        {
       
                gameState = STATE_PLAYING;

                _background.setTexture(this->_data->asset.GetTexture("Background"));

                _data->asset.LoadTexture("Background", MAIN_MENU_BACKGROUND_FILEPATH);
               
                _data->asset.LoadTexture("Player 1 Down Frame 1", PLAYER_1_SPRITE_DOWN_1);
                _data->asset.LoadTexture("Player 1 Down Frame 2", PLAYER_1_SPRITE_DOWN_2);
                _data->asset.LoadTexture("Player 1 Down Frame 3", PLAYER_1_SPRITE_DOWN_3);
                _data->asset.LoadTexture("Player 1 Down Frame 4", PLAYER_1_SPRITE_DOWN_4);
                _data->asset.LoadTexture("Player 1 Down Frame 5", PLAYER_1_SPRITE_DOWN_5);
                _data->asset.LoadTexture("Player 1 Down Frame 6", PLAYER_1_SPRITE_DOWN_6);
                _data->asset.LoadTexture("Player 1 Down Frame 7", PLAYER_1_SPRITE_DOWN_7);
                _data->asset.LoadTexture("Player 1 Down Frame 8", PLAYER_1_SPRITE_DOWN_8);
                _data->asset.LoadTexture("Player 1 Down Frame 9", PLAYER_1_SPRITE_DOWN_9);
                _data->asset.LoadTexture("Player 1 Down Frame 10", PLAYER_1_SPRITE_DOWN_10);
                _data->asset.LoadTexture("Player 1 Down Frame 11", PLAYER_1_SPRITE_DOWN_11);
                _data->asset.LoadTexture("Player 1 Down Frame 12", PLAYER_1_SPRITE_DOWN_12);
                _data->asset.LoadTexture("Player 1 Down Frame 13", PLAYER_1_SPRITE_DOWN_13);
                _data->asset.LoadTexture("Player 1 Down Frame 14", PLAYER_1_SPRITE_DOWN_14);
                _data->asset.LoadTexture("Player 1 Down Frame 15", PLAYER_1_SPRITE_DOWN_15);
                _data->asset.LoadTexture("Player 1 Down Frame 16", PLAYER_1_SPRITE_DOWN_16);
                _data->asset.LoadTexture("Player 1 Down Frame 17", PLAYER_1_SPRITE_DOWN_17);

                _data->asset.LoadTexture("Player 1 Up Frame 1", PLAYER_1_SPRITE_UP_1);
                _data->asset.LoadTexture("Player 1 Up Frame 2", PLAYER_1_SPRITE_UP_2);
                _data->asset.LoadTexture("Player 1 Up Frame 3", PLAYER_1_SPRITE_UP_3);
                _data->asset.LoadTexture("Player 1 Up Frame 4", PLAYER_1_SPRITE_UP_4);
                _data->asset.LoadTexture("Player 1 Up Frame 5", PLAYER_1_SPRITE_UP_5);
                _data->asset.LoadTexture("Player 1 Up Frame 6", PLAYER_1_SPRITE_UP_6);
                _data->asset.LoadTexture("Player 1 Up Frame 7", PLAYER_1_SPRITE_UP_7);
                _data->asset.LoadTexture("Player 1 Up Frame 8", PLAYER_1_SPRITE_UP_8);
                _data->asset.LoadTexture("Player 1 Up Frame 9", PLAYER_1_SPRITE_UP_9);
                _data->asset.LoadTexture("Player 1 Up Frame 10", PLAYER_1_SPRITE_UP_10);
                _data->asset.LoadTexture("Player 1 Up Frame 11", PLAYER_1_SPRITE_UP_11);
                _data->asset.LoadTexture("Player 1 Up Frame 12", PLAYER_1_SPRITE_UP_12);
                _data->asset.LoadTexture("Player 1 Up Frame 13", PLAYER_1_SPRITE_UP_13);
                _data->asset.LoadTexture("Player 1 Up Frame 14", PLAYER_1_SPRITE_UP_14);
                _data->asset.LoadTexture("Player 1 Up Frame 15", PLAYER_1_SPRITE_UP_15);
                _data->asset.LoadTexture("Player 1 Up Frame 16", PLAYER_1_SPRITE_UP_16);
                _data->asset.LoadTexture("Player 1 Up Frame 17", PLAYER_1_SPRITE_UP_17);

                player1 = new Player1(_data);
        }

        void GameState::HandleInput()
        {
                sf::Event event;

                while (this->_data->window.pollEvent(event))
                {
                        if (sf::Event::Closed == event.type)
                        {
                                this->_data->window.close();
                        }

                }
        }

        void GameState::Update(float dt)
        {
                player1->Animate(dt);
        }

        void GameState::Draw(float dt)
        {
                _data->window.clear(sf::Color::Red);

                _data->window.draw(this->_background);

                player1->Draw();

                _data->window.display();
        }
}

Player 1.h

#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
#include "Definitions.h"
#include "GameLoop.h"
namespace Cobra
{
        class Player1
        {
        public:
                Player1(GameDataRef data);

                void Init();
                void HandleInput();
                void Update();
                void Draw();
                void Animate(float dt);

        private:
                GameDataRef _data;

                sf::Sprite _player1;

                std::vector<sf::Texture> _animationFrames;

                unsigned int _animationIterator;

                sf::Clock _clock;
        };
}

Player 1.cpp

#include <iostream>
#include "Player 1.h"
#include "Definitions.h"
namespace Cobra
{
        Player1::Player1(GameDataRef data) : _data(data)
        {

                _animationIterator = 0;

                /// Player 1 Down Frames
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 1"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 2"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 3"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 4"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 5"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 6"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 7"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 8"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 9"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 10"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 11"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 12"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 13"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 14"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 15"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 16"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Down Frame 17"));

                /// Player 1 Up Frames
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 1"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 2"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 3"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 4"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 5"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 6"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 7"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 8"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 9"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 10"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 11"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 12"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 13"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 14"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 15"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 16"));
                _animationFrames.push_back(_data->asset.GetTexture("Player 1 Up Frame 17"));

                _player1.setTexture(_animationFrames.at(_animationIterator));

                /// Player 1 Scalling
                _player1.setScale(sf::Vector2f(4.0f, 4.0f));
        }

        void Player1::Draw()
        {
                _data->window.draw(_player1);
        }

        void Player1::Animate(float dt)
        {
                /// Player 1 Down
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                {
                        _player1.move(sf::Vector2f(0.0f, 3.0f));

                        if (_clock.getElapsedTime().asSeconds() > PLAYER_1_ANIMATION_DURATION / _animationFrames.size())
                        {
                        if (_animationIterator < _animationFrames.size() - 1)
                        {
                                _animationIterator++;
                        }
                        else
                        {
                                _animationIterator = 0;
                        }

                                _player1.setTexture(_animationFrames.at(_animationIterator));

                                _clock.restart();
                        }
                }
               
                if (!sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                {
                        _player1.setTexture(this->_data->asset.GetTexture("Player 1 Down Frame 1"));
                }

                /// Player 1 Up
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                {
                        _player1.move(sf::Vector2f(0.0f, -3.0f));
                }
        }
}

I believe that this is all of the code that applies to my little (very big) problem. Any help at all will be greatly appreciated, thanks to everyone!

P.S. I know about Thor but I would rather code my own than use someone else's (if that makes any sense), just wanted to mention it in case it gets brought back up.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
P.S. I know about Thor but I would rather code my own than use someone else's (if that makes any sense), just wanted to mention it in case it gets brought back up.

Let me try to explain this with a metaphor... There is a difference between someone who is proficient in a language and can communicate "with his/her own words" and a person who tries to communicate by making up words. You NEED to look at how Thor does it so that you can first become proficient in the language you are trying to communicate with, because right now your code doesn't look comprehensible. I mean no offense, I've been there too, and learned from people who know what they're doing.