<< Chapter < Page
  Xna game studio     Page 8 / 11
Chapter >> Page >

Except for the call to the DirectionVector method, the code in Listing 14 is straightforward and should not require an explanation beyond the embedded comments. I will explain the DirectionVector method shortly.

Instantiate and set properties on the power pills and the UFOs

Listing 15 uses essentially the same code to instantiate and set the property values on the power pill sprites and the UFO sprites.

Listing 15 . Instantiate and set properties on the power pills and the UFOs.

//Use the same process to instantiate all of the // power pills and cause them to move from right// to left, top to bottom. for(int cnt = 0;cnt<numPills;cnt++) { pills.Add(new Sprite("ball",Content,random));pills[cnt].Position = new Vector2((float)(windowWidth * random.NextDouble()), (float)(windowHeight * random.NextDouble()));pills[cnt].Direction = DirectionVector((float)maxVectorLength, (float)(maxVectorLength * random.NextDouble()),true,//xNeg false);//yNegpills[cnt].WindowSize =new Point(windowWidth,windowHeight); pills[cnt].Speed = maxPillSpeed/2 + maxPillSpeed * random.NextDouble()/2;}//end for loop //Use the same process to instantiate all of the// ufos and cause them to move from right to left, // bottom to top.for(int cnt = 0;cnt<numUfos;cnt++) { ufos.Add(new Sprite("ufo",Content,random));ufos[cnt].Position = new Vector2((float)(windowWidth * random.NextDouble()), (float)(windowHeight * random.NextDouble()));ufos[cnt].Direction = DirectionVector((float)maxVectorLength, (float)(maxVectorLength * random.NextDouble()),true,//xNeg true);//yNegufos[cnt].WindowSize =new Point(windowWidth,windowHeight); ufos[cnt].Speed = maxUfoSpeed/2 + maxUfoSpeed * random.NextDouble()/2;}//end for loop }//end LoadContent

The private DirectionVector method

The DirectionVector method shown in Listing 16 is called each time the code in Listing 14 and Listing 15 needs to set the Direction property on a sprite. This method is declared private because it has no meaning outside the Sprite class.

Listing 16 . The private DirectionVector method.

private Vector2 DirectionVector(float vecLen, float xLen,Boolean negX, Boolean negY){Vector2 result = new Vector2(xLen,0); result.Y = (float)Math.Sqrt(vecLen*vecLen- xLen*xLen); if(negX)result.X = -result.X; if(negY)result.Y = -result.Y; return result;}//end DirectionVector

Return a direction vector

The DirectionVector method returns a direction vector as type Vector2 given the length of the vector, the length of the X component of the vector, the sign of the X component, and the sign of the Ycomponent.

The signs of the components

You should set negX and/or negY to true to cause them to be negative.

By adjusting the signs on the X and Y components, the vector can be caused to point into any of the four quadrants. The relationships between these two valuesand the direction of motion of the sprite are as follows:

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Xna game studio. OpenStax CNX. Feb 28, 2014 Download for free at https://legacy.cnx.org/content/col11634/1.6
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Xna game studio' conversation and receive update notifications?

Ask