Saturday, April 24, 2010

Simple sprite animation using Qt

I was always wondering how character are animated in games so I thought to create simple animated character using Qt.

Technique is quite simple, you need to draw sequence of image with different character position.
To hold image of different character position I am using QList.

mLeftSprite.append( QPixmap(":left_1.png"));
mLeftSprite.append( QPixmap(":left_2.png"));
mLeftSprite.append( QPixmap(":left_3.png"));
mLeftSprite.append( QPixmap(":left_4.png"));
mLeftSprite.append( QPixmap(":left_5.png"));
mLeftSprite.append( QPixmap(":left_6.png"));  

To animate character I have used timer which timeout after every 100ms and I am displaying next caracter image when timer expire and also updating character position on screen at same time

mCurrentFrame = ++mCurrentFrame % 6;
mPos.setX( mPos.x() + mXDir * 10 ); 
And finally draw image on screen

So end result is like following.

If you are instrested in looking at code than here is link to gitorious.

No comments:

Post a Comment