How it works...

In this recipe, we place the animated sprite object in the middle of the window and set its image source to the sprite sheet that we have just added to the project resource. Then, we count how many frames there are in the sprite sheet that belong to the running animation, which in this case is 11 frames. We also inform Qt regarding the dimension of each frame of the animation, which in this case is 128 x 128. After that, we set the frame rate to 25 to get a decent speed and then set it to loop infinitely. We then set the running value to true so that the animation will play by default when the program starts running.

Then, in step 4, we want to be able to pause the animation and resume it by clicking on the window. We simply check whether the sprite is currently paused when clicking on the mouse area. If the sprite animation is paused, then the animation resumes; otherwise, the animation is paused.

In step 6, we replace anchors.centerIn with x and y values so that the animated sprite object is not anchored to the center of the window, which would make it impossible to move around. Then, we create a number animation within the animated sprite to animate its x property. We set the start value to somewhere outside the window on the left side, and we set the end value to somewhere outside the window on the right side. After that, we set the duration to 3,000 milliseconds (3 seconds) and make it loop infinitely.

Lastly, we also set the running value to true so that it plays the animation by default when the program starts running.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset