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 - Lyna

Pages: [1]
1
Graphics / Re: [bug] Multi-file Sprite sheet animation help
« on: October 08, 2015, 11:09:27 pm »
Updated the query

2
Graphics / Re: [bug] Multi-file Sprite sheet animation help
« on: October 08, 2015, 12:27:21 pm »
Edit:// Found out that the program isn't even reading the input code, that may be the error

You have your input code in the constructor of "PlayerCharacterSprite". Is this intended?

What would be the best way to handle input for this project? with an input Manager class?

3
Graphics / [Help] Multi-file Sprite sheet animation help (Update)
« on: October 07, 2015, 09:59:09 pm »
Hello, I am a beginner in using SFML and I am trying to have a sprite class that can be used to draw the same sprite to multiple/ different game-screens. So far the code draws the sprite to the desired game-screen, but I am struggling with having the player sprite move and change image (using a spritesheet, i.e. change sprite to face another direction/ cycle through a short animation).

I have drawn the sprite from it's class, to the desired game-screen, but how can i update the sprite to move and change direction?
Here is the spritesheet:


Here is the code.
//main.cpp
#include <iostream>
#include "ScreenManager.h"
#include "PlayerCharacterSprite.h"
#include <SFML/Graphics.hpp>

using namespace std;

int main()
{
        sf::RenderWindow Window(sf::VideoMode(ScreenWidth, ScreenHeight, 32), "Platformer v1");

        ScreenManager::GetInstance().Initialize();
        ScreenManager::GetInstance().LoadContent();

        while (Window.isOpen())
        {
                sf::Event Event;
                while (Window.pollEvent(Event))
                {
                        switch (Event.type)
                        {
                        case sf::Event::Closed:

                                break;
                        case sf::Event::KeyPressed:
                                if (Event.key.code == sf::Keyboard::Escape)
                                {
                                        Window.close();
                                }
                        }
                }

                if (Event.key.code == sf::Keyboard::Up)
                {
                        //make player move and change sprite (up)
                        std::cout << "Up" << std::endl;
                }
                else if (Event.key.code == sf::Keyboard::Down)
                {
                        //make player move and change sprite (up)
                        std::cout << "Down" << std::endl;
                }
                else if (Event.key.code == sf::Keyboard::Right)
                {
                        //make player move and change sprite (up)
                        std::cout << "Right" << std::endl;
                }
                else if (Event.key.code == sf::Keyboard::Left)
                {
                        //make player move and change sprite (up)
                        std::cout << "Left" << std::endl;
                }

                Window.clear();

                ScreenManager::GetInstance().Update();
                ScreenManager::GetInstance().Draw(Window);

                Window.display();
        }

        return 0;
}

//PlayerCharacterSprite.cpp
#include "PlayerCharacterSprite.h"
#include <iostream>

PlayerCharacterSprite::PlayerCharacterSprite()
{
        enum Direction { Down, Left, Right, Up }; //0, 1, 2, 3
        sf::Vector2i source(1, Down); // SF way of doing -> int sourceX = 32, sourceY = Down;

        if (!playerTexture.loadFromFile("charSpriteSheet.png"))
                std::cout << "Error could not load player image" << std::endl;
       
       

        playerSprite.setTexture(playerTexture);
        playerSprite.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32)); //
}

void PlayerCharacterSprite::AnimSprite()
{
        //source.x++;
        //if (source.x * 32 >= playerTexture.getSize().x) //rotates through the sprite cycle
        //      source.x = 0;

        //return;
}

//PlayerCharacterSprite.h
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include "ScreenManager.h"

class PlayerCharacterSprite
{
public:
        PlayerCharacterSprite();
        void AnimSprite();
       
        sf::Sprite& getSprite() { return playerSprite; }

        sf::Vector2i source; // SF way of doing -> int sourceX = 32, sourceY = Down;
       

protected:
private:
        sf::Texture playerTexture;
        sf::Sprite playerSprite;
        sf::Event Event;
};

 

Trying to update the sprite on this page:
//MgStarterArea.cpp
#include "MgStarterArea.h"


MgStarterArea::MgStarterArea()
{
       
}


MgStarterArea::~MgStarterArea()
{
}

void MgStarterArea::LoadContent()
{
        /*if (!font.loadFromFile("arial_0.ttf"))
        {
                std::cout << "Could not find font" << std::endl;
        }
        sentence = "Starter Area";*/


}

void MgStarterArea::UnloadContent()
{

}

void MgStarterArea::Update()
{

}

void MgStarterArea::Draw(sf::RenderWindow &Window)
{
       
        /*sf::Text text(sentence, font, 40);
        text.setColor(sf::Color(255, 255, 255));*/


        PlayerCharacterSprite player;
        Window.draw(player.getSprite());
        Window.draw(text);
}
 

Result:

Pages: [1]
anything