X hits on this document

283 views

0 shares

0 downloads

0 comments

52 / 102

{

   spriteBatch = screenSpriteBatch;

   projectileStartPosition = startPosition;

   textureName = TextureName;

   isAI = isAi;

   hitOffset = groundHitOffset;

   gravity = Gravity;

}

The above constructors simply initializes the class’s various fields and will be later used by the catapult class to create new projectiles.

122.

Override the DrawableGameComponent’s Initialize method to load the projectile’s texture:

C#

public override void Initialize()

{

   // Load a projectile texture

projectileTexture = curGame.Content.Load<Texture2D>(textureName);

}

123.

Override the DrawableGameComponent’s Draw method:

C#

public override void Draw(GameTime gameTime)

{

   spriteBatch.Draw(projectileTexture, projectilePosition, null,

       Color.White, projectileRotation,

       new Vector2(projectileTexture.Width / 2,

                   projectileTexture.Height / 2),

       1.0f, SpriteEffects.None, 0);

   base.Draw(gameTime);

}

The draw method is written so that the projectile can be rotated by updating the “projectileRotation” field.

124.

We will now add the most important projectile method, the one which updates the projectile while it is in flight:

C#

public void UpdateProjectileFlightData(GameTime gameTime, float wind, float gravity, out bool groundHit)

{

   flightTime += (float)gameTime.ElapsedGameTime.TotalSeconds;

   // Calculate new projectile position using standard

   // formulas, taking the wind as a force.

   int direction = isAI ? -1 : 1;

Page | 52

Document info
Document views283
Page views283
Page last viewedFri Dec 09 14:27:49 UTC 2016
Pages102
Paragraphs3115
Words17333

Comments