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

Pages: [1]
1
Hi,

Thankyou for the quick reply.

leftAnimation is an object of my Animation class.

Thanks I will double check my pointers.

Kind regards,
starrybolt.

2
Hi,

I am currently learning Object Orientated Programming and I am attempting to use an animation class that will be able to be used with every sprite which I will need in my SFML game.

I am a beginner trying to implement this class (https://github.com/SFML/SFML/wiki/Source:-AnimatedSprite) to make my game object orientated and further understand how it works.

I have two separate .cpp files: AnimateSprite.cpp and AnimatedSprite.cpp.

Here is code from the AnimateSprite.cpp file.

#include "AnimateSprite.h"
#include "AssetManager.h"

Animation::Animation() : m_texture(NULL)
{

}

void Animation::addFrame(sf::IntRect rect)
{
    m_frames.push_back(rect);
}

void Animation::setSpriteSheet(const sf::Texture& texture)
{
    m_texture = &texture;
}

const sf::Texture* Animation::getSpriteSheet() const
{
    return m_texture;
}

std::size_t Animation::getSize() const
{
    return m_frames.size();
}

const sf::IntRect& Animation::getFrame(std::size_t n) const
{
    return m_frames[n];
}
 


and here is a minimised version of code from my AnimatedSprite.cpp file.
void AnimatedSprite::setAnimation(const Animation& animation)
{
    m_animation = &animation;
    m_texture = m_animation->getSpriteSheet();
    m_currentFrame = 0;
    setFrame(m_currentFrame);
}

void AnimatedSprite::play(const Animation& animation)
{
    if (getAnimation() != &animation)
        setAnimation(animation);
    play();
}

const Animation* AnimatedSprite::getAnimation() const
{
    return m_animation;
}

void AnimatedSprite::setFrame(std::size_t newFrame, bool resetTime)
{
    if (m_animation)
    {
        //calculate new vertex positions and texture coordiantes
        sf::IntRect rect = m_animation->getFrame(newFrame);

        m_vertices[0].position = sf::Vector2f(0.f, 0.f);
        m_vertices[1].position = sf::Vector2f(0.f, static_cast<float>(rect.height));
        m_vertices[2].position = sf::Vector2f(static_cast<float>(rect.width), static_cast<float>(rect.height));
        m_vertices[3].position = sf::Vector2f(static_cast<float>(rect.width), 0.f);

        float left = static_cast<float>(rect.left) + 0.0001f;
        float right = left + static_cast<float>(rect.width);
        float top = static_cast<float>(rect.top);
        float bottom = top + static_cast<float>(rect.height);

        m_vertices[0].texCoords = sf::Vector2f(left, top);
        m_vertices[1].texCoords = sf::Vector2f(left, bottom);
        m_vertices[2].texCoords = sf::Vector2f(right, bottom);
        m_vertices[3].texCoords = sf::Vector2f(right, top);
    }

    if (resetTime)
        m_currentTime = sf::Time::Zero;
}

void AnimatedSprite::update(sf::Time deltaTime)
{
    // if not paused and we have a valid animation
    if (!m_isPaused && m_animation)
    {
        // add delta time
        m_currentTime += deltaTime;

        // if current time is bigger then the frame time advance one frame
        if (m_currentTime >= m_frameTime)
        {
            // reset time, but keep the remainder
            m_currentTime = sf::microseconds(m_currentTime.asMicroseconds() % m_frameTime.asMicroseconds());

            // get next Frame index
            if (m_currentFrame + 1 < m_animation->getSize())
                m_currentFrame++;
            else
            {
                // animation has ended
                m_currentFrame = 0; // reset to start

                if (!m_isLooped)
                {
                    m_isPaused = true;
                }

            }

            // set the current frame, not reseting the time
            setFrame(m_currentFrame, false);
        }
    }
}

void AnimatedSprite::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    if (m_animation && m_texture)
    {
        states.transform *= getTransform();
        states.texture = m_texture;
        target.draw(m_vertices, 4, sf::Quads, states);
    }
}

 

The unhandled exception is thrown at this line of code:

leftAnimation->setSpriteSheet(playerLeft);

 void Animation::setSpriteSheet(const sf::Texture& texture)
{
    m_texture = &texture;
}
 

This then takes me too my setSpritesheet function within my animation class.



In the load content function of the game.cpp this is how the playerLeft texture is loaded in.


  if(!playerLeft.loadFromFile("left_spritesheet.png"))
    {
        std::cout << "Could not load texture from file" << std::endl;
    }

 

Can anyone please advise?
Any help would be greatly appreciated.

Many thanks,
starrybolt.

3
Hi,

I am currently learning Object Orientated Programming and I am attempting to use an animation class that will be able to be used with every sprite which I will need in my game.

 leftAnimation->setSpriteSheet(playerLeft);

 void Animation::setSpriteSheet(const sf::Texture& texture)
{
    m_texture = &texture;
}
 

The above line of code, throws the unhandled exception error taking me to the setSpritesheet function in the animation class.

In the load content function of the game.cpp this is how the playerLeft texture is loaded in.

 
        if(!playerLeft.loadFromFile("left_spritesheet.png"))
        {
                std::cout << "Could not load texture from file" << std::endl;
        }
 

I am a beginner trying to implement this class (https://github.com/SFML/SFML/wiki/Source:-AnimatedSprite) to make my game object orientated and further understand how it works.

Can anyone please advise?

Many thanks,
starrybolt.

4
Hi.

Yes it was within the game loop, Im sorry I forgot to mention that.
Thankyou Arcade and Nexus, I appreciate your help as this has been bothering me for a couple of days now.
I apologise for my stupid errors, I am rather new to SFML.

Kind regards,
starrybolt.

5
Hi.
Thankyou for the reply.
Yes I did alot of research before posting but everything I found and implemented did not work correctly and the key sprite was still showing after collision.
Okay thankyou.

6
Hello.
I am currently developing a platformer game using SFML 2.0 and C++ (VS 2010)

The aim of this game is to collect all the keys within the time frame.
I have managed to get the sprite to collide with the keys however I am unable to be able to remove the sprite from the screen when the keys are collected. On suggestion from my lecturer I tried to make the sprite transparent, currently shown in code below, however once collision has happened between the sprites then the key reappears.
Does anyone have a solution in which I can remove the sprite completely?
Or even just setting it transparent after collision?

All help is appreciated.
Many thanks.
starrybolt.

Here is my code:

bool collSpritePos = false;

                //creating the texture
                sf::Texture keyTexture;
                //loading the file
                keyTexture.loadFromFile("keysprite.png");
                //creating a vector with an array of 7 sprites
                std::vector<sf::Sprite> keySprites(7, sf::Sprite(keyTexture));
                //for loop to check through array and set sprite positions
                for(int i = 0; i < keySprites.size(); i++)
                {
                        keySprites[0].setPosition(460, 80);
                        keySprites[1].setPosition(140, 275);
                        keySprites[2].setPosition(800, 300);
                        keySprites[3].setPosition(700, 500);
                        keySprites[4].setPosition(125, 500);
                        keySprites[5].setPosition(250, 850);
                        keySprites[6].setPosition(700, 800);
                }

                bool keySpriteVisible = true;

                //collision detection for key sprite with player sprite

                if(keySprites[6].getGlobalBounds().intersects(playerSprite.getGlobalBounds()))
                {
                        collSpritePos = true;
                        keySpriteVisible = false;
                        std::cout << "COLLISION" << std::endl;
                        if(!keyMusic.openFromFile("coinMusic.ogg"))
                        {
                                std::cout << " Error loading 'coinMusic' file." << std::endl;
                        }

                        keyMusic.play();

                        if(keySpriteVisible == false)
                        {
                                keySprites[6].setColor(sf::Color::Transparent);
                        }
                }
 

Pages: [1]