XNa Study Notes-6-further use of game components and make them work

Source: Internet
Author: User

The last time I got scared .. this time, it is worth noting that the last time I explained what the game components are and how to add them, it is far from enough because it is the second core of the game, so today, let's use it and let it play its role.

First, create a project and add a game component, renamed gc1.cs

I believe everyone will do it now (if not, please take a look at it once );

And modify his code to this

Then we will start to study the role of this compoment.

Add the following code to gc1. I will explain it later.

 

Note! I believe that those two pieces of code are surprisingly familiar to me several times before "drills". Yes, he is one of the most important void, draw, and loadcontent in the game, there are also updates at the bottom of it. These functions are the same. here we can see that the game components are an independent part of the game, but they belong to the whole, just like controls and forms.

However, some applications are still different from those of game. Here I will introduce the differences:

Declare 2 variables first

Add:

Texture2d text2d;

Spritebatch SBC;

Then modify the loadcontent method as follows.

 

 

////// Note that windtile1 is an image of the current user. You can enter a name based on the image in the content. If you have any questions, refer to the previous articles. For more information, see.

 

 

Note: The spritebatch variables have been defined in game, but the game components are not described in advance, so you have to define them yourself.

Careful people may also find that the value assignment statement of text2d is slightly different. There is a game before the content? Because game. Content is the only space for storing game data, it is best to write it here.

Then, the last step is taken. Now this component is not called and should not be instantiated yet.

Add the following statement to game1.cs:

 

They mean

Instantiate gc1

Add it to compoments to read and call

Last modified code

 

 

This is very important. This indicates that gc1 instantiates the container that belongs to it, that is, the game itself "This". Now run the game and check that a texture is displayed on the screen!

This is just a rough understanding of the game components. Its powerful functions can only be explored by us, it can be regarded as a single subject with logic, space, and permissions. Therefore, using it can not only make the game organized, but also speed up the processing of the game. The code is concise and clear.

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.