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

Ayele, K., 2003. Introductory Economics, 3rd ed., Addis Ababa.
Widad Reply
can you send the book attached ?
Ariel
?
Ariel
What is economics
Widad Reply
the study of how humans make choices under conditions of scarcity
AI-Robot
U(x,y) = (x×y)1/2 find mu of x for y
Desalegn Reply
U(x,y) = (x×y)1/2 find mu of x for y
Desalegn
what is ecnomics
Jan Reply
this is the study of how the society manages it's scarce resources
Belonwu
what is macroeconomic
John Reply
macroeconomic is the branch of economics which studies actions, scale, activities and behaviour of the aggregate economy as a whole.
husaini
etc
husaini
difference between firm and industry
husaini Reply
what's the difference between a firm and an industry
Abdul
firm is the unit which transform inputs to output where as industry contain combination of firms with similar production 😅😅
Abdulraufu
Suppose the demand function that a firm faces shifted from Qd  120 3P to Qd  90  3P and the supply function has shifted from QS  20  2P to QS 10  2P . a) Find the effect of this change on price and quantity. b) Which of the changes in demand and supply is higher?
Toofiq Reply
explain standard reason why economic is a science
innocent Reply
factors influencing supply
Petrus Reply
what is economic.
Milan Reply
scares means__________________ends resources. unlimited
Jan
economics is a science that studies human behaviour as a relationship b/w ends and scares means which have alternative uses
Jan
calculate the profit maximizing for demand and supply
Zarshad Reply
Why qualify 28 supplies
Milan
what are explicit costs
Nomsa Reply
out-of-pocket costs for a firm, for example, payments for wages and salaries, rent, or materials
AI-Robot
concepts of supply in microeconomics
David Reply
economic overview notes
Amahle Reply
identify a demand and a supply curve
Salome Reply
i don't know
Parul
there's a difference
Aryan
Demand curve shows that how supply and others conditions affect on demand of a particular thing and what percent demand increase whith increase of supply of goods
Israr
Hi Sir please how do u calculate Cross elastic demand and income elastic demand?
Abari
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