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

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