The problem I have is that when the position of player is the same as the touch position then I have a nasty filckering (player moves up for one moment then moves down, and keep going as long as I have the pressed event going on). I tried to eliminate the flickering but I can't figure out what is going on. Any ideas or a better preffered way will be much welcomed.
public override void Update(GameTime gameTime)
{
#region Input
NinjaGame.Instance.TouchCollection = TouchPanel.GetState();
foreach (TouchLocation tl in NinjaGame.Instance.TouchCollection)
{
if (tl.State == TouchLocationState.Pressed ||
tl.State == TouchLocationState.Moved)
{
float playerPosHalfHeight = this.Position.Y + (this.Texture.Height/2);
// Something fishy is going on here...
if (tl.Position.Y >= playerPosHalfHeight)
this.Velocity = new Vector2(0f, 0.25f);
if (tl.Position.Y < playerPosHalfHeight)
this.Velocity = new Vector2(0f, -0.25f);
}
if (tl.State == TouchLocationState.Released)
{
this.Velocity = new Vector2(0f, 0f);
}
}
#endregion
base.Update(gameTime);
/* In other words...
float delta = (float)gameTime.ElapsedGameTime.Milliseconds;
Position += new Vector2(Velocity.X * delta, Velocity.Y * delta);
*/
#region Screen bounds check (top / bottom)
if (this.Position.Y + this.Texture.Height > NinjaGame.Instance.GraphicsDevice.Viewport.Height)
this.Position = new Vector2(this.Position.X,
NinjaGame.Instance.GraphicsDevice.Viewport.Height -
this.Texture.Height);
if (this.Position.Y < 0f)
this.Position = new Vector2(this.Position.X, 0f);
#endregion
}
Thanks, happy coding.