IT tutorials
 
Mobile
 

XNA Game Studio 3.0 : Making a Prettier Clock with 3-D Text (part 2)

11/5/2011 3:44:32 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

3. Other Loop Constructions

C# also provides two other loop constructions, called do – while and while. These are not actually vital, in that you can always get the looping behavior that you want by using an appropriately designed for loop, but they can be useful in situations where you don’t want to go to the trouble of creating a for loop construction. You can find out more about these kinds of loops and when they would be useful in the glossary in the do – while entry.

4. Fun with for Loops

You can test your understanding of the for loop behavior by looking at some for loops and trying to work out what they would do. For instance, look at this one:

for (layer = 0 ; layer > 4 ; layer++)

There’s a mistake in this statement, but it’s rather hard to spot. The mistake is that the test is now layer > 4. The > character means "greater than." This means that the test is now true only when the value of layer is greater than 4. Because the initialization sets the value of layer to 0, this condition is never true. The result is that the code in the statement controlled by the loop is never performed. Now look at this statement:

for (layer = 0 ; layer < 4 ; layer--)

There’s another mistake here. The less-than character (<) is in the correct place, but rather than increasing the value of layer each time around, the change makes layer smaller by using the -- operator each time. This means that the value of layer never becomes greater than 4, so the loop never ends. The result is that your program appears to "get stuck" at this point.

You can write code to request this as follows if you really want a loop that goes on forever:

for (layer = 0 ; true ; layer--)

Simply putting the value true in the position of the condition causes the loop to never stop. If you’re wondering what would happen if you ran a loop like this, you can try it if you like, but I can save you the trouble. If you run either of these never-ending loops, you eventually get the message shown in Figure 5. This is the message that XNA displays when it runs out of memory.

Figure 5. Out-of-memory error message


The reason you get this message is that each time the DrawString method is called in the body of the loop, it uses a small amount of memory to record what is drawn. If you call the method a large number of times, it eventually uses up all the memory available for this purpose, and the memory allocation part of XNA throws an exception when it is asked for memory it doesn’t have. The good news is that this doesn’t cause any damage, but it does cause serious damage to your credibility.

One of the nice things about loops is that you can get a lot more work done by the computer simply by changing the values that cause them to stop. For instance, look at this code:

for (layer = 0; layer < 40 ; layer++)

This version of the loop draws 40 red time values before putting the yellow one on top. It gives rise to the rather funky display shown in Figure 6.

Figure 6. Funky time


This is nice, but you can do even better. You can make the display even more funky by using some other drawing tricks that XNA provides.

 
Others
 
- XNA Game Studio 3.0 : Making a Prettier Clock with 3-D Text (part 1)
- Android Views (part 3) - CheckBoxes, RadioButtons, and Spinners
- Android Views (part 2) - Button and ImageButton
- Android Views (part 1) - TextView and EditText
- Java ME on Symbian OS : Networking and General Connection Framework (part 2) - Security Policy for Network Connections & NetworkDemo MIDlet
- Java ME on Symbian OS : Networking and General Connection Framework (part 1)
- jQuery 1.3 : Effects - Basic hide and show
- jQuery 1.3 : Effects - Inline CSS modification
- Mobile Web Apps : Events
- Mobile Web Apps : Setting up Shop
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us