<< Chapter < Page | 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 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 .
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
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
Notification Switch
Would you like to follow the 'Xna game studio' conversation and receive update notifications?