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

How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
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
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
How can I make nanorobot?
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
how can I make nanorobot?
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
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?