using user-defined data type as data-member of a class.
-
Here is a class called Animation, and it has a function called Update as shown in the code below.
class Animation
{
private:
Vector2u imageCount; // contains the number of total images in row and total no. of coloumns , in this case (3, 9)
Vector2u currentImage; // acts as the limiter.
float totalTime; // the total time taken by whole animation.
float switchTime; // the time duration btw to images.public:
// Texture * texture -> takes the whole image.
// Vector2u imageCount -> counts the number of images in each row, and total number of coloumn.
// float switchTime -> time btw 2 images.
Animation(Texture* texture, Vector2u imageCount, float switchTime);
~Animation();IntRect uvRect; // sets the height, width, top, and left corner of the image to be printed on the screen. void Update(int row, float deltaTime, bool faceRight ); // float deltaTime -> gives the FPS of the machine, acc. to which the switch bte images is maintained.
};
Animation::Animation(Texture* texture, Vector2u imageCount, float switchTime) {
this->imageCount = imageCount;
this->switchTime = switchTime;
totalTime = 0.0f;// here we have set the initial image position-> first position currentImage.x = 0; currentImage.y = 0; // here we are setting height & width of each image. uvRect.width = texture->getSize().x / static\_cast(imageCount.x); uvRect.height = texture->getSize().y / static\_cast(imageCount.y);
}
Animation::~Animation()
{
}// this is the function i am talking about
// it takes argument which are the data members of player class.
void Animation::Update(int row, float deltaTime, bool faceRight) {
currentImage.y = row;
totalTime += deltaTime; // deltaTime is added to the totalTime.if (totalTime >= switchTime) { totalTime -= switchTime; currentImage.x++; // every time totalTime >= switchTime, incrementing currentImage.x if (currentImage.x >= imageCount.x) { // when currentImage.x >= imageCount.x, the currentImage.x = 0. currentImage.x = 0; } } uvRect.top = currentImage.y \* uvRect.height; // to turn the player to each side, left or right. if (faceRight) { uvRect.left = currentImage.x \* uvRect.width; // if the faceRight is True. uvRect.width = abs(uvRect.width) ; } else { uvRect.left = (currentImage.x + 1) \* abs(uvRect.width); // if the faceRight is false . uvRect.width = -abs(uvRect.width); }
}
and the is 2nd class called player, in which i have