Time for action – using animations to move items smoothly
In order to use it, we add a new private member called m_animation
of type QPropertyAnimation
and initialize it in the constructor of Player
:
m_animation = new QPropertyAnimation(this); m_animation->setTargetObject(this); m_animation->setPropertyName("jumpFactor"); m_animation->setStartValue(0); m_animation->setKeyValueAt(0.5, 1); m_animation->setEndValue(0); m_animation->setDuration(800); m_animation->setEasingCurve(QEasingCurve::OutInQuad);
What just happened?
For the instance of QPropertyAnimation
created here, we define the item as 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 Player
class—and the property that should be animated—jumpFactor
, in this case. Then we define the start and the end value of that property, and in addition to that we also define a value in between by...