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

Author Topic: Animation playing an extra frame  (Read 1065 times)

0 Members and 1 Guest are viewing this topic.

Raphman

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Animation playing an extra frame
« on: August 18, 2012, 06:31:42 pm »
Hey guys, I'm new to using SFML, and I was trying to get a sprite to animate through some frames. I've done this before in XNA and never had much of a problem with it.  However when I'm doing it now, it plays one extra frame for some reason

I adapted the c# animated sprite class that can be found on the wiki here : https://github.com/SFML/SFML/wiki/SpriteAnimated  into c++ and it seems to me like I'm doing everything correctly, and I just can't see where the problem is.

Here's my AnimatedSprite class:
#include "animatedsprite.h"
#include <iostream>

using namespace std;
AnimatedSprite::AnimatedSprite(Texture text, float frameWidth, float frameHeight, int fps,int firstFrame, int lastFrame,bool isAnimated = false, bool isLooped = true)
{
        _text = text;
        _frameWidth = frameWidth;
        _frameHeight = frameHeight;
        _fps = fps;
        _interval = _fps/250;
        _firstFrame = firstFrame;
        _lastFrame = lastFrame;
        _frameCount = lastFrame - firstFrame;
        _currentFrame = _firstFrame;
        _isAnimated = isAnimated;
        _isLooped = isLooped;
        _clock = 0;
        setTexture(_text);
        setTextureRect(getFramePositon(_currentFrame));
}


IntRect AnimatedSprite::getFramePositon(int frame)
{
        int count = (getTexture()->getSize()).x/ _frameWidth;
        int newX = frame% count;
        int newY = frame/count;
        IntRect pos(_frameWidth*newX,_frameHeight*newY,_frameWidth,_frameHeight);
        return pos;
}

void AnimatedSprite::update(float deltaTime)
{
        _clock += deltaTime;
       
        if(_clock >= _interval && _isAnimated)
        {
                IntRect temp = getFramePositon(_currentFrame);
                setTextureRect(temp);

                if (_currentFrame < _lastFrame)
                {
                        _currentFrame++;
                }
                else
                {
                        _currentFrame = _firstFrame;
                }

                _clock = 0;
        }

        if(!_isLooped && _currentFrame == _lastFrame)
        {
                _isAnimated = false;
        }
}

float AnimatedSprite::getFPS()
{
        return _fps;
}

void AnimatedSprite::setFPS(float value)
{
        _fps = value;
}


and here's my main:

#include <SFML/Graphics.hpp>
#include <iostream>
#include "AnimatedSprite.h"
using namespace sf;

using namespace std;

int main()
{
        RenderWindow window(VideoMode(1280, 720), "Mazian Chronicles");
        window.setFramerateLimit(60);
        Texture text;
        Clock deltaClock;
        Time dt;
        if(!text.loadFromFile("sprite.png"))
        {
                return EXIT_FAILURE;
        }

        AnimatedSprite  crono(text,33,35,60,0,12,true,true);
        while (window.isOpen())
        {
                Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == Event::Closed)
                                window.close();
                }


               
                dt =deltaClock.restart();
                crono.update(dt.asSeconds());
                window.clear();
                window.draw(crono);
                window.display();
        }

        return 0;
}

I would be very appreciative if someone could help me with my (most likely stupid) mistake. Thanks!


Raphman

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Animation playing an extra frame
« Reply #1 on: August 18, 2012, 06:34:13 pm »
Oh god, do I feel silly, not two seconds after I posted I found the mistake.  I wanted twelve frames so I had it go 0-12 which would be 13 frames. I changed it to 11 and it works. Sorry for the needless post.  :'(
« Last Edit: August 18, 2012, 06:41:19 pm by Raphman »

 

anything