Problem Getting sprite to animate with SFML

So this is my first try animating on my own, without a reference right in front of me. It's not working, I did check some of the projects I've made in the past, but for one those were done with Allegro, and really it should be able to move over to SFML quite easily, but it's not. It does display on the window in the correct place, but there are 3 pictures in the file and I want it to cycle through them, but it shows all three at the same time, and it only works once. if I press space a second time, only one of the pictures shows up, and it doesn't cycle at all. Here is my .cpp file for it, can anyone tell if anything is missing? or what it is I need to change? Any help would be greatly appreciated. (if more info is needed please let me know)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "headers.h"
#include "SwordSprite.h"


	int top = 0;
	int left = 88;
	int right = 132;
	int bottom = 35;

SwordSprite::SwordSprite()
{
	Load("images/aGameSwordSpriteSheet.png");
	assert(IsLoaded());
}

SwordSprite::~SwordSprite()
{
}

void SwordSprite::Update(float elapsedTime)
{
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
	{
		int curFrame = 0;
	    int maxFrame = 2;

		for(curFrame = 0; curFrame <= maxFrame; curFrame++)
		{
			GetSprite().setTextureRect(sf::IntRect(left, top, right, bottom));
			GetSprite().setOrigin((GetSprite().getTextureRect().width / 2) - 9.0f, (GetSprite().getTextureRect().height / 2) + 2.0f);

			left -= 44;
			right -= 44;
					
			VisibleGameObject::Update(elapsedTime);	
		}
		curFrame = 0;
	
	}

	left = 88;
	right = 132;
}

void SwordSprite::Draw(sf::RenderWindow &swin)
{
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
	{
		VisibleGameObject::Draw(swin);		
	}
}
Last edited on
Maybe something like this
1
2
3
4
5
6
for(int curFrame = 0; curFrame <= maxFrame; curFrame++)
{
	GetSprite().setTextureRect(sf::IntRect(SPRITE_WIDTH * curFrame, 0, SPRITE_WIDTH, SPRITE_HEIGHT ));

	VisibleGameObject::Update(elapsedTime);	
}
oh that's a good idea, I was thinking of doing it like that somehow but I couldn't think of how to do it, thank you, I will try this and let you know if it works.
Topic archived. No new replies allowed.