Part 1: Basic knowledge (Chapter 1) An xNa mobile phone Program (continued)

Source: Internet
Author: User
Tags silverlight

Spritefont1.spritefont the file name is not very vivid. We recommend that you rename it with the actual font it describes. If you need to continue using the default font settings, You can rename it to segoe14.spritefont. Right-click the file name and select Properties to view the properties of the file-you can see the name of the asset, that is, the file name without the extension: sagoe14. The asset name points to the font you loaded in the program. If you change the asset name to a name independent from the file name, you will be confused in future use and cannot find your font.

In the initial phase, the xnahellophone project contains two C # code files: program. CS and game1.cs. The first file is very simple and has nothing to do with the Windows Phone 7 game program! The program class can be activated only after the windows or Xbox tag is defined. When the Windows Phone program is compiled, replace the windows_phone flag.

For many games, you spend almost all your time on the game1.cs file. The game1 class inherits from the game class. In its original state, it defines two fields: graphics and spritebatch. I will add three more fields based on these two fields:

XNa project:XnahellophoneFile:Game1.cs(Excerpt showing fields)

Namespace xnahellophone

{

Public class game1: Microsoft. xNa. Framework. Game

{

Graphicsdevicemanager graphics;

Spritebatch;

String text = "Hello, Windows Phone 7! ";

Spritefont segoe14;

Vector2 textposition;

...

}

}

The three new fields briefly indicate what text will be displayed, the font of the text displayed, and the position of the text displayed on the screen. This position is specified relative to the pixel coordinates in the upper left corner of the display. The vector2 structure has two fields that specify the floating point type of X and Y. For performance considerations, all the floating point values of xNa are single-precision. (Silverlight is double-precision .) The vector2 structure is usually used for two-dimensional points, sizes, and even carriers.

When the game is running on a mobile phone, the game1 class is instantiated and the game1 constructor is also executed. The standard code is as follows:

XNaProject:XnahellophoneFile:Game1.cs(Excerpt)

Public game1 ()

{

Graphics = new graphicsdevicemanager (this );

Content. rootdirectory = "content ";

// Frame rate is 30 FPS by default for Windows Phone.

Targetelapsedtime = timespan. fromticks (333333 );

}

The first statement initializes the graphic field. In the second statement, content is the game attribute, which belongs to the contentmanager type and rootdirectory is the attribute of this class. Set this attribute to "content", which is the content directory that currently stores the 14 point segoe font. The third statement sets the game cycle time of the program, which manages the frequency of updating the video display by the program. The screen refresh frequency for Windows Phone 7 is 30 frames per second.

After game1 is instantiated, the run method is called in the game1 instance. The base class of the game class starts the process of "game startup. One of the first steps is to call the initialize method, and the game's derivatives can be rewritten. XNa game studio automatically generates the framework method. I didn't add any content:

XNaProject:XnahellophoneFile:Game1.cs(Excerpt)

Protected override void initialize ()

{

Base. initialize ();

}

The initialization method is not where the font or other content is loaded. This is the method used by the base class to call loadcontent later.

XNaProject:XnahellophoneFile:Game1.cs(Excerpt)

Protected override void loadcontent ()

{

Spritebatch = new spritebatch (graphicsdevice );

Segoe14 = This. content. Load ("Segoe14 ");

Vector2 textsize = segoe14.measurestring (text );

Viewport = This. graphicsdevice. viewport;

Textposition = new vector2 (viewport. Width-textsize. X)/2,

(Viewport. Height-textsize. Y)/2 );

}

The first statement in this method is automatically provided by the system. You will soon see how to use this spritebatch object to display sub-images (sprites ).

Other statements are added by me. You will see that I tend to use this keyword as the prefix of attributes such as content and graphicsdevice to remind myself that they are attributes rather than static classes. As mentioned above, the attribute of content is of the contentmanager type. The general load method allows you to load content to a program. In this case, the attribute of content is of the spritefont type. The name enclosed in quotation marks is the name of the asset in the content attribute. This statement is used to store the font loaded in the spritefont-type segoe14 field.

In xNa, the child image (sprites) (including text strings) is usually displayed by specifying the pixel coordinates relative to the upper left corner or relative to the upper left corner of the Child image (sprites. To calculate these coordinates, it is best to know the screen size and the specific font size when the text is displayed.

The spritefont class has a very convenient method for measuring the size, named measurestring, which returns the size of the vector2 object and a specific text string in pixels. (14-pound segoe UI mono font, which is equivalent to 18-2/3 pixels in height. After measurestring is called, the height of 28 pixels is returned .) (For the 14-point segoe UI mono font, which has an equivalent height of 18-2/3 pixels, The measurestring call returns a height of 28 pixels .)

The xNa program usually uses the viewport attribute of the graphicsdevice class to obtain the screen size. This is accessed through the graphicsdevice attribute of the game and provides the width and height attributes.

Then, the textposition -- point corresponding to the top left corner of the viewport is calculated as the upper left corner of the displayed text string. (It is then straightforward to calculate textposition-the point relative to the upper-left corner of the viewport where the upper-left corner of the text string is to be displayed .)

So far, the initialization phase of the program has been completed and substantive operations have actually started. The program enters the game loop. During Video Display Synchronization at a rate of 30 frames per second, the two methods in the program are constantly called: update and draw: update, draw, update, draw, update, draw ...... (If each update method takes more than 1/30 seconds to complete this process, it is more complicated than previously described, but we will discuss these timing issues in detail in the following chapters .)

The draw method allows you to draw on the display screen. But this is what you want to do. If you need to execute some calculations to prepare for the drawing, you should first execute the update method. The update method is the preparation program of the draw method. In many cases, the xNa program moves the position of the sub-screen on the display screen based on user input. For mobile phone users, this user input mainly involves touch the screen with your fingers. All processing of user input should also be performed in the update method. You will see the example in Chapter 3rd.

You should write your own update and draw methods so that they can be executed as soon as possible. This is quite obvious, but I guess some very important factors may not be very obvious:

You should avoid the update and draw code from the local heap periodically ?) . (You shoshould avoid code in update and draw that routinely allocates memory from the program's local heap .) eventually ,. net garbage collector will reclaim part of this memory, and when the garbage collector does this, your game may be stuck (your game might stutter a bit ). In the section on xNa programming, you can see how to avoid heap ?) .

Your draw method may not contain any suspicious Code; it is usually lurking in the update method, resulting in various troubles. Avoid any New Expressions Involving classes. These will cause memory allocation. The instantiation structure is acceptable, but (however is a bit strange and does not seem to be a turning relationship. Instantiating a structure is fine, however, because structure instances are stored on the stack and not in the heap.) because the Schema Instantiation is stored on the stack rather than heap ?). (XNa uses more structures rather than classes in Object Type Definitions. You often need to create them in update .) However, without a new explicit expression, heap may also occur ?) Allocate. For example, when two strings are connected ?) Create another string. If you need to skillfully execute string operations in the update method, use stringbuilder. In short, xNa provides a method to display text using the stringbuilder object.

However, in xnahellophone, the update method is negligible. The text displayed by the program is fixed at a specific position. All necessary calculations have been executed in the loadcontent method. Therefore, you do not need to make any changes to the update method, but simply keep the appearance of the xNa game studio when it was initially created:

XNaProject:XnahellophoneFile:Game1.cs(Excerpt)

Protected override void Update (gametime)

{

If (gamepad. getstate (playerindex. One). Buttons. Back = buttonstate. Pressed)

This. Exit ();

Base. Update (gametime );

}

The Default Code uses the static game board (gamepad) class to check whether the back button of the mobile phone hardware is pressed and use this function key to exit the game.

Finally, there is the draw method. The following code creates a light blue version with a single background color:

XNaProject:XnahellophoneFile:Game1.cs(Excerpt)

Protected override void draw (gametime)

{

Graphicsdevice. Clear (color. cornflowerblue );

Base. Draw (gametime );

}

In the xNa programming community, cornflowerblue becomes its iconic color. When you develop an xNa program, the appearance of the light blue screen is very comfortable, because this means that the program is just as you have used the draw method.

However, if you want to save power and display it on an OLED, you will get a darker background. In the modified version, I compromised to set the background to dark blue. In Silverlight, xNa supports the Standard 140 colors. After the following code is executed, the text is displayed in White:

XNaProject:XnahellophoneFile:Game1.cs(Excerpt)

Protected override void draw (gametime)

{

Graphicsdevice. Clear (color. Navy );

Spritebatch. Begin ();

Spritebatch. drawstring (segoe14, text, textposition, color. White );

Spritebatch. End ();

Base. Draw (gametime );

}

The child screen (sprites) on the display is bundled into a spritebatch object, which is created when loadcontent is called. There can be multiple call drawstring between the call start (BEGIN) and end (end) to draw text and bitmap. These are the only options. This specific cable calls the position of the referenced font, the text to be displayed, the screen, and the upper left corner of the color relative to the upper left corner of the text.

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.