<< 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

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
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