Property animations
We implemented the horizontal movement using a QTimer
. Now, let's try a second way to animate items—the Animation framework.
Time for action - Using animations to move items smoothly
Let's add a new private member called m_jumpAnimation
of the QPropertyAnimation *
type, and initialize it in the constructor of MyScene
:
m_jumpAnimation = new QPropertyAnimation(this); m_jumpAnimation->setTargetObject(this); m_jumpAnimation->setPropertyName("jumpFactor"); m_jumpAnimation->setStartValue(0); m_jumpAnimation->setKeyValueAt(0.5, 1); m_jumpAnimation->setEndValue(0); m_jumpAnimation->setDuration(800); m_jumpAnimation->setEasingCurve(QEasingCurve::OutInQuad);
What just happened?
For the instance of QPropertyAnimation
created here, we define the item as a parent; thus, the animation will get deleted when the scene deletes the item, and we don't have to worry about freeing the used memory. Then, we define the target of the animation—our MyScene
class—and the property...