2.6 answers
I hope this chapter will not have many difficulties. This chapter focuses on two main issues: Unit Testing and sprites. Maybe you still have some problems running the game on Xbox 360. I have written some tips and tips at the end of Chapter 1. You can go and have a look.
Another problem is to understand why an exception is thrown and how to solve it. Most people do not have this problem, but it is a problem for a few people who are not familiar with exceptions, especially xNa exceptions. You must keep an eye on the exception and check the complete stack trace to see if there is any internal exception with detailed descriptions.
Similar to hosted DirectX, sometimes you encounter underlying DirectX framework exceptions and sometimes it is messy. You can go to the Internet and msdn to find corresponding errors.Code. In these cases, context information is more important than the specific error information itself. For example, after a shader fails to be compiled, you will get a strange error code, in this case, you should check whether the shader file exists, whether it can be correctly compiled, and whether your hardware can handle it, rather than trying to understand what the error code means. You can use the nvidia fx composer tool to check whether the shader file is valid.
If you have any questions when using unit testing, you can refer to the "answers" section in the next chapter, where you will discuss many questions about unit testing. Remember that unit tests and standardProgramThere is no difference. You need to debug the final applications and track them step by step.
Finally, there are some tips for using sprites in xNa:
- If you want to render a lot of sprites on top of other things, you will encounter many random sorting order problems and different texture problems, in addition, the background sprite sometimes masks everything before it. As you can see in this xNa pong game, a separate spritebatch call process is used to render the background so that it is independent, it does not interfere with the rendering of menus and Sprite in the game.
-
- To render all Sprite at a time, especially when you can sort them in hybrid mode. Calling one spritebatch to render all the sprite at a time will make it much faster. For more information, see the drawsprites method in pong games. If you use different blending modes, such as light effect processing using additive, and other sprite using Alpha hybrid, you need to separate two processes for rendering, first render the Alpha hybrid element, and then add the light effect of the addition operation on the top, so that your game performance will increase by 200%-300%.
-
- To solve the resolution problem and how to handle different widths and heights, you can obtain the current resolution in the initialize method. The Code is as follows:Width=Graphics. graphicsdevice. viewport. width;
Height=Graphics. graphicsdevice. viewport. height;
- You cannot force a specific resolution because it may not work properly on Xbox 360 and can only be a preset resolution on Windows. For example, if you want to test the resolution of 720 p (1024 × 768) on your PC, you must have a display that supports this resolution, which can be a window mode) or full screen mode ). Add the following code in the constructor of the game main class (in this chapter, the ponggame class) to set the resolution to 720 p, and use the above method in the initialize method to obtain the width and height to check whether it is correct.Graphics. preferredbackbufferwidth= 1280;
Graphics. preferredbackbufferheight= 720;
-
- XNa does not support Font Processing. To display text, you can use a texture image that contains text in this chapter, or write your own Font Processing System as in chapter 5. For both solutions, you must use sprite to render text or render a letter each time.
- In xNa, Sprite is not the only way to present 2D data. You can write your own shaders and even do more advanced effects than the sprite class itself. As an example, you can see how to use shaders to render 2D lines in chapter 5.
2.7 summary of this Chapter
I hope that you will like the content in this chapter and master the entire process of creating a game. Pong may not be the most fun game, but it covers everything from start to end that can be included in any game project. For larger game projects, only one chapter is not enough, but here you have learned at least some concepts, unit tests, and some basic terms of Agile Methodology. In the next chapter, you need to learn about the design of helper classes, and then study the game classes and game components in depth. After that, you can prepare a 3D engine containing many cool special effects, it will be used in the last part of this book to use xNa to write a big, fun game. At the same time, you will also learn some other small but fun game projects.
The most important thing in this chapter is the benefits of agile development for game programming. Even if you can make some changes to the game in real time, there is a very easy and rigorous way to solve the problem, but first make a game idea-even if there is only one page of paper, still important. In addition, unit testing is also very helpful, but in the next chapter you will see that unit testing can also be completed automatically, unit tests are also useful for some helper classes used by other projects in this book.
Take a look at what you have learned in this chapter:
- Writing down your ideas and having a small game idea is very important
- Keeping your game ideas on one page is helpful.
- Use rigorous steps to solve the problem, and use unit tests at the highest level of the problem without considering the specific implementation details. In this way, you can quickly extract the key part of the game idea and write a unit test. At this time, you can easily find out what method you want to implement.
- Create Texture Materials, load them into the project, and then render them as Sprite
- Rendering all Sprite at a time can improve Game Performance
- Using a suitable formula can help you create a resolution-independent game so that the game can run well on Xbox 360 at any resolution
- Learn how to use xact to add sound effects. Chapter 9th provides more details.
- Process game input to support two gamepad controllers and multiple player keyboard operations
- Use the boundingbox structure for 2D Collision Detection
- If you always set the Z value to 0, you can use vector3 instead of vector2.
- It is very important to use unit tests for collision detection and greatly reduce your testing workload. Even if you are currently processing a completely different part from the collision detection, you can switch to the collision detection unit test for another detection.