<< Chapter < Page
  Xna game studio     Page 6 / 13
Chapter >> Page >

This version of the Sprite class supports collision detection based on intersecting rectangles. It also provides a new Edge property that records and returns the edge number (1, 2, 3, or 4) if a spritecollides with an edge of the game window. However, the edge information is available for only one iteration of the game loop before it changes back to thenormal value of 0.

The value of the Edge property temporarily changes to 1, 2, 3, or 4 if a sprite collides with the top, right, bottom, or left edge of thegame window respectively.

Since the information regarding the collision with an edge is the only change to this Sprite class relative to the version that I explained in a previous module, that is all I will discuss about the Sprite class in this module.

The new code

The first manifestation of change in this version of the Sprite class is the simple declaration of an instance variable of type int named edge near the beginning of the class definition. You can view that statement in Listing 13 .

A new read-only property accessor method for the Edge property

The next manifestation is the read-only property accessor method for the new Edge property shown in Listing 4 .

Listing 4 . A new read-only property accessor method for the Edge property for the Sprite class.

public int Edge { get {return edge; }//end get}//end Edge property accessor

New version of the Move method

Listing 5 shows the new version of the Move method.

Listing 5 . New version of the Move method.

//This method causes the sprite to move in the // direction of the direction vector if the elapsed// time since the last move exceeds the elapsed // time target based on the specified speed.public void Move(GameTime gameTime) { //New to this version//Clear the Edge property value. Edge information // is available for only one iteration of the// game loop. edge = 0;//Accumulate elapsed time since the last move. elapsedTime +=gameTime.ElapsedGameTime.Milliseconds; if(elapsedTime>elapsedTimeTarget) { //It's time to make a move. Set the elapsed// time to a value that will attempt to produce // the specified speed on the average.elapsedTime -= elapsedTimeTarget; //Add the direction vector to the position// vector to get a new position vector. position = Vector2.Add(position,direction);//Check for a collision with an edge of the game // window. If the sprite reaches an edge, cause// the sprite to wrap around and reappear at the // other edge, moving at the same speed in a// different direction within the same quadrant // as before. Also set the Edge property to// indicate which edge was involved. 1 is top, 2 // is right, 3 is bottom, and 4 is left.// Note that the Edge property will be cleared //to 0 the next time the Move method is called. if(position.X<-image.Width) { position.X = windowSize.X;edge = 4;//collision with the left edge - new NewDirection();}//end if if(position.X>windowSize.X) { position.X = -image.Width / 2;edge = 2;//collision with the right edge - new NewDirection();}//end if if(position.Y<-image.Height) { position.Y = windowSize.Y;edge = 1;//collision with the top - new NewDirection();}//end if if(position.Y>windowSize.Y) { position.Y = -image.Height / 2;edge = 3;//collision with the bottom - new NewDirection();}//end if on position.Y }//end if on elapsed time}//end Move

Questions & Answers

Application of nanotechnology in medicine
what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
Damian
yes that's correct
Professor
I think
Professor
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
LITNING Reply
what is a peer
LITNING Reply
What is meant by 'nano scale'?
LITNING Reply
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
if virus is killing to make ARTIFICIAL DNA OF GRAPHENE FOR KILLED THE VIRUS .THIS IS OUR ASSUMPTION
Anam
analytical skills graphene is prepared to kill any type viruses .
Anam
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
Praveena Reply
hi
Loga
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





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