Skip to Main Content
Learning XNA 3.0
book

Learning XNA 3.0

by Aaron Reed
November 2008
Beginner content levelBeginner
510 pages
16h 24m
English
O'Reilly Media, Inc.
Content preview from Learning XNA 3.0

Randomly Generating Different Sprite Types

To randomly generate sprites of different types, you need to first determine the likelihood that each type will be created. Most of the sprites in this game will be AutomatedSprites. ChasingSprites will be the next most common, and EvadingSprites will show up only occasionally. In this section, you'll be assigning a percentage likelihood to each type of sprite. Each time a new sprite is spawned, you'll determine which type of sprite to create based on those percentages. The exact percentage likelihood for each sprite type is something that you can play with as you test your game, and you can adjust the values to the point where they feel right to you, the developer.

To begin, open the SpriteManager class and add three new class-level variables representing the likelihood that each sprite type will be spawned:

int likelihoodAutomated = 75;
int likelihoodChasing = 20;
int likelihoodEvading = 5;

You'll notice that the values added equal 100 (representing 100%). Essentially, 75% of the time you'll generate an AutomatedSprite, 20% of the time a ChasingSprite, and 5% an EvadingSprite.

Now, you have to add some code that will generate a random number, and, based on the value of that random number, create one of the three sprite types. Open your SpriteManager class and replace this call to SpriteList.Add, which is at the end of the SpawnEnemy method:

spriteList.Add( new EvadingSprite (Game.Content.Load<Texture2D>(@"images\skullball"), position, new ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning XNA 4.0

Learning XNA 4.0

Aaron Reed
Beginning C# 7 Programming with Visual Studio 2017

Beginning C# 7 Programming with Visual Studio 2017

Benjamin Perkins, Jacob Vibe Hammer, Jon D. Reid

Publisher Resources

ISBN: 9780596154905Supplemental ContentErrata Page