Making a bouncing ball
In this recipe, you will make a bouncing ball; when clicking a button, a ball falls towards the ground and on touching the ground, it bounces back to the top. In this recipe, you will understand how a basic animation is applied on an object. This recipe will consist of a push button and a ball, and when the push button is pressed, the ball will start animating towards the ground.
How to do it...
To make a ball appear to be bouncing, we need to make it first animate towards the ground, and then from the ground up to the sky. To do so, we will be invoking the setKeyValueAt
method of the QPropertyAnimation
class three times. The first and second calls to the setKeyValueAt
method will make the ball animate from the top to the bottom. The third call to the setKeyValueAt
method will make the ball animate from bottom to top. The coordinates in the three setKeyValueAt
methods are provided so that the ball bounces in the opposite direction, and not where it came from. The following...