belt.avapose.com

.NET/Java PDF, Tiff, Barcode SDK Library

Figure 2-8. Two overlapping boxes To test your method, you ll create a second, standing sprite in the middle of the window. To do this, you need to replicate the sprite-creation code and include the code for testing collisions in the Update method of the Game1 class. First, include the sprite s variable definition at the beginning of the Game1 class, along with the previous sprite definition. clsSprite mySprite2;

winforms pdf 417 reader, winforms qr code reader, winforms upc-a reader, winforms data matrix reader, winforms gs1 128, winforms ean 13 reader, c# remove text from pdf, itextsharp replace text in pdf c#, winforms code 39 reader, c# remove text from pdf,

The big win in using dependency injection is that it allows you to make your applications loosely coupled. That is to say, any one class of your implementation will tend not to have any dependencies on any other class s specific implementation.

Now, in the LoadContent method, include the code for the sprite creation and set its starting velocity: mySprite2 = new clsSprite(Content.Load<Texture2D>("xna thumbnail"), new Vector2(218f, 118f), new Vector2(64f, 64f), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); mySprite2.velocity = new Vector2(3, -3); In the UnloadContent method, include the code for disposing of the sprite: mySprite2.texture.Dispose(); In the Update method, include the code to move the second sprite: mySprite2.Move(); Finally, in the Draw method, include the code for drawing the new sprite. The code for drawing the two sprites follows: spriteBatch.Begin(SpriteBlendMode.AlphaBlend); mySprite1.Draw(spriteBatch); mySprite2.Draw(spriteBatch); spriteBatch.End(); If you run the program now, you ll see both sprites, but they aren t bouncing yet. You can make them bounce by including a call to the Collides method in the Update method and changing the velocity between the sprites, as follows: if (mySprite1.Collides(mySprite2)) { Vector2 tempVelocity = mySprite1.velocity; mySprite1.velocity = mySprite2.velocity; mySprite2.velocity = tempVelocity; } In this code, you store the velocity of mySprite1 in the tempVelocity variable, set the velocity of mySprite1 to the velocity to mySprite2, and then set the velocity of mySprite2 to tempVelocity, thus changing the velocity between the sprites. If you run the code now, you ll see the sprites moving and bouncing against each other and against the window borders, as shown in Figure 2-9. Although the collision is detected using the bounding-box algorithm, after some tests, you will see a problem: if the boxes collide diagonally, the circles will bounce before they really hit each other. When testing for collisions between circles, you can simply check if the distance between the circle centers are less than the sum of their radius. If it is, there is a collision. This provides a precise way to test for circle collisions.

You will still have dependencies on the type system that you re establishing in your application, of course, but loose coupling encourages the use of programming to interfaces rather than to abstract or concrete implementations.

Figure 2-9. The sprites now move and collide. To change your clsSprite code to support collisions between two circle sprites, create two new read-only properties, center and radius, which are calculated according to the other sprite properties. public Vector2 center{ get{ return position + (size/2);} } // Sprite center public float radius { get { return size.X / 2; } } // Sprite radius Next, create a new method for testing this specific type of collision: public bool CircleCollides(clsSprite otherSprite) { // Check if two circle sprites collided return (Vector2.Distance(this.center, otherSprite.center) < this.radius + otherSprite.radius); } Finally, change the Update method of the Game1 class to call CircleCollides instead of Collides. You ll see that the circles will now bounce only when they actually collide.

Rather than talking in abstract terms, I will show you an example of some tightly coupled code to illustrate these concerns (see Listing 3-1).

If you search for XNA in any common Internet search engine, you ll get (as of February 2009) around four million hits. When you narrow down the search to XNA Tutorial, you ll get about half a million results, without quotation marks, and about thirty thousand results with them. So, forget about searching the Internet for your next steps, unless you know exactly what you need! Riemer Grootjans presents a variety of XNA tutorials that may help you go a step further in exploring new XNA horizons at his site (http://www.riemers.net). We also recommend Riemer s excellent XNA 3.0 Game Programming Recipes book (Apress, 2009).

   Copyright 2020.