- Game Programming using Qt 5 Beginner's Guide
- Pavel Strakhov Witold Wysota Lorenz Haas
- 320字
- 2021-08-27 18:31:13
Time for action - Keeping multiple animations in sync
Now we'll start implementing the coin class. We can use a simple QGraphicsEllipseItem object, but we'll need to animate its properties, so let's create a new Coin class and derive it from QObject and QGraphicsEllipseItem. Define two properties: opacity of the qreal type and rect of the QRect type. This is done only by the following code:
class Coin : public QObject, public QGraphicsEllipseItem
{
Q_OBJECT
Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)
Q_PROPERTY(QRectF rect READ rect WRITE setRect)
//...
};
No function or slot was added, because we simply used built-in functions of QGraphicsItem and associated them with the properties.
Next, we'll create the explode() function that will start some animations when the player collects the coin. Create a Boolean private field in the class and use it to ensure that each coin can only explode once:
void Coin::explode()
{
if (m_explosion) {
return;
}
m_explosion = true;
//...
}
We want to animate our two properties by two QPropertyAnimation objects. One fades the coin out, while the other scales the coin in. To ensure that both animations get started at the same time, we use QParallelAnimationGroup, as follows:
QPropertyAnimation *fadeAnimation =
new QPropertyAnimation(this, "opacity");
//... QPropertyAnimation *scaleAnimation = new QPropertyAnimation(this, "rect");
//... QParallelAnimationGroup *group = new QParallelAnimationGroup(this); group->addAnimation(scaleAnimation); group->addAnimation(fadeAnimation); connect(group, &QParallelAnimationGroup::finished, this, &Coin::deleteLater); group->start();