X hits on this document

296 views

0 shares

0 downloads

0 comments

66 / 102

           // gesture samples

           if (null != firstSample)

           {

               Vector2 delta = prevSample.Value.Position -

                   firstSample.Value.Position;

               Catapult.ShotVelocity = MinShotStrength +

                   Catapult.ShotStrength *

                   (MaxShotStrength - MinShotStrength);

               Catapult.Fire(Catapult.ShotVelocity);

               Catapult.CurrentState = CatapultState.Firing;

           }

           // turn off dragging state

           firstSample = null;

           prevSample = null;

           isDragging = false;

       }

   }

}

While lengthy, the above method is rather simple. In order to fire, the user will drag a finger across the device’s display and the shot’s strength will be calculated based on the distance between the point where the user first touched the screen to the point where the user lifted the finger from the display. This is exactly what the above method is responsible for, all the while updating a variable which will be used to draw visual feedback as the user drags the finger across the display.

144.

Finally, we will override the Draw method and add an additional helper method named DrawDragArrow. These methods will display an arrow on screen which depicts the strength of the shot as the user drags a finger across the display:

C#

public override void Draw(GameTime gameTime)

{

   if (isDragging)

       DrawDragArrow(arrowScale);

   base.Draw(gameTime);

}

public void DrawDragArrow(float arrowScale)

{

   spriteBatch.Draw(arrow, catapultPosition + new Vector2(0, -40),

       null, Color.Blue, 0,

Page | 66

Document info
Document views296
Page views296
Page last viewedSun Dec 11 00:31:24 UTC 2016
Pages102
Paragraphs3115
Words17333

Comments