Auxiliary class-other auxiliary classes

Source: Internet
Author: User
Other auxiliary classes

The helpers namespace contains more helper classes. Most helper classes are as simple as the randomhelper class. Traversing them is boring, so please review the auxiliary classes not mentioned in this chapter. If you want to learn more about them, you can use the unit tests contained in them for testing.

Before entering the breakout game at the end of this chapter, browse several remaining helper classes that will be frequently used in the following chapters: spritehelper, enumhelper, and colorhelper.

Spritehelper class

In the previous chapter, you used sprite rendering multiple times because unit testing forces you to process Sprite in xNa in a simple way. This solution, as well as what you may actually useCodeMore than once into reusable classes, which leads to the use of the spritehelper class (3-10 ). It mainly provides a constructor to create a spritehelper instance to store texture textures and graphic rectangle image rectangular data. It also provides some rendering methods to easily draw sprite to the screen, as you did with a series of spritetorender classes in the previous chapter.


Figure 3-10

Most methods do not perform many operations here. The constructor only initializes the value. The render method only adds a new spritetorender instance to the sprite list. The rendercentered method shows the Sprite in the center of the specified position, finally, the drawsprites method draws all Sprite to the screen. Let's take a look at the drawsprites method. It is similar to the drawsprites method in the previous chapter, but there are some improvements:

 Public static void drawsprites (INT width, int height) {// No need to render if we got no sprites this frame if (sprites. count = 0) return; // create sprite batch if we have not done it yet. // use device from texture to create the sprite batch. if (spritebatch = NULL) spritebatch = new spritebatch (sprites [0]. texture. graphicsdevice); // start rendering sprites spritebatch. begin (spriteblendmode. alphablend, spritesortmode. backtofront, savestatemode. none); // render all sprites foreach (spritetorender Sprite in sprites) spritebatch. draw (sprite. texture, // rescale to fit resolution new rectangle (sprite. rect. x * width/1024, Sprite. rect. y * Height/768, Sprite. rect. width * width/1024, Sprite. rect. height * Height/768), Sprite. sourcerect, Sprite. color); // we are done, draw everything on screen. spritebatch. end (); // kill list of remembered sprites. clear ();} // drawsprites (width, height) 

This method is called with the current width and height of the rendering window resolution as the parameter, and all Sprite scales proportionally according to the resolution, which is very important to support all resolutions of the Xbox 360. In the drawsprites method, first check whether something is to be rendered. Then make sure that the static spritebatch object is created and will be used by all Sprite to be drawn. After the spritebatch begin method is called, traverse all the Sprite in the current frame and adjust their sizes to adapt to the current screen. Finally, call the end method to draw each sprite to the screen. The sprite list is also cleared and the next frame is refreshed. As an example of how this class works, refer to the breakout game at the end of this chapter.

Enumhelper class

The enumhelper class (shown in 3-11) is useful when you need to traverse enumeration items or quickly obtain the serial number of enumeration values. Pong and breakout games do not use any enumeration type, but the next chapter of Games use the enum class (system. Enum) to traverse the block type. Note that the enumhelper class uses several methods of the enum class, which are not implemented in. NET Compact framework. To avoid any compilation errors, the entire enumhelper class is usually excluded in the Xbox 360 Project, but you can still use it on Windows if you like.


Figure 3-11

The testgetallenumnames Method Used for unit testing is shown as follows. It also shows how the getallenumnames method works. It uses the enumenumerator helper class defined inside the enumhelper class to traverse each enumerated value.

[Test] public void testgetallenumnames () {assert. areequal ("missions, highscore, credits, help, options, exit, back", enumhelper. getallenumnames (typeof (menubutton);} // testgetallenumnames ()

The getallenumnames method uses the writearraydata auxiliary method in the stringhelper class discussed earlier:

 
Public static string getallenumnames (type) {return stringhelper. writearraydata (getenumerator (type);} // getallenumnames (type)
Colorhelper class

The colorhelper class (as shown in Figure 3-12) is long and has many methods. However, because the new color in xNa is similar to the system used in managed DirectX. the color class in drawings is much more powerful, so many methods are no longer needed. However, it still contains some useful methods for color operations.


Figure 3-12

For example, the colorhelper. Empty field can be used to set the shader special effect parameter to a null value -- 0, 0, 0 is generally not a valid color value; it is completely transparent. The black Alpha value is 255.

 
/// <Summary> // empty color, used to mark unused color values. /// </Summary> Public static readonly color empty = new color (0, 0, 0, 0 );

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.