XNA Foundation (03) animation and frame rate

Source: Internet
Author: User

We want to do 2D and 3D games inseparable from animation, then in the XNA how to achieve animation?

First, let's look at the simplest animation--moving.

To move a sprite is very simple, we only need to change the position coordinates of the sprite in the Game1.update () method, and the sprite displayed on the screen will be moved the next time the Game1.draw () method is called.

Next, we look at a more complex animation, such as the explosion of a bomb, that we can do to make a series of pictures, each of which is the performance of a state during the explosion, as follows:

The 20 small pictures above represent all the states of an explosion from the beginning to the end, and in actual production, we usually make these pictures in order in a large image, and ensure that the size of each small figure in the larger image is exactly the same. We call this large graph the sprite frame sequence diagram Sprite Sheets.

With the explosion of the sprite Sheets, we can change the index of the next small picture (such as [2,3]) in the Game1.update () method, and then calculate the relative position of the small picture that will be displayed in the sprite Sheets according to the index and the size of the small picture. So that we can draw the area of the sprite sheets in the Game1.draw () method. You should remember the third parameter sourcerectangle of the Game1.draw () method mentioned in the previous article, which allows you to specify the target area where we want to draw the sprite sheets.

It seems that implementing an animation is not difficult, the real difficulty lies in how to control each animation can have its own different refresh speed, how to make the same animation on different configurations of the machine performance. This involves the frame rate problem.

Frame Rate, which refers to the number of redraw screens in a second. The default frame rate set for us by the XNA Frame is 60fps. Why did you choose 60? Because this is the lowest refresh rate of the monitor in the case where the person's eyes do not feel blinking. We can reset it through the property targetelapsedtime of the base class Microsoft.Xna.Framework.Game. Like what:

base.TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 10);

This means that every 10ms is redrawn once, that is, the frame rate is 100fps.

Setting the frame rate to 100fps does not mean that it will actually reach 100fps, which depends on how the machine is configured. When the machine is not configured enough, XNA automatically skips certain times of drawing-that is, the Game1.draw () method is not invoked. We can detect whether the actual frame rate is smaller than we set by gametime (you remember that both the update and the draw methods have a parameter of that type) Isrunningslowly property.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.