Sign in to follow this  
locoflop

XNA Touch Movement

Recommended Posts

Hello, I have a piece of code where I would like to move the player up and down with touch input. If the touch is made just above the player position then player moves up and the same goes for opposite direction.

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.

[CODE]
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... :P
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

}
[/CODE]

Thanks, happy coding.

Share this post


Link to post
Share on other sites
Touch devices are usually quite imprecise since they're trying to map a (relatively) massive finger (or thumb) to a really tiny point. Its likely jumping around because your finger is wiggling and causing the dot the touch device mapped your finger to move about.

Easiest way to fix this would be to create sort of a dead zone over top where your character is. So instead of:

[code] if (tl.Position.Y >= playerPosHalfHeight)
this.Velocity = new Vector2(0f, 0.25f);

if (tl.Position.Y < playerPosHalfHeight)
this.Velocity = new Vector2(0f, -0.25f);[/code]

Try something along the lines of:

[code] if (tl.Position.Y >= TopOfPlayerPosition)
this.Velocity = new Vector2(0f, 0.25f);

if (tl.Position.Y < BottomofPlayerPosition)
this.Velocity = new Vector2(0f, -0.25f);[/code]

This should create a dead zone over the area that the player character occupies where the touch input doesn't do anything, but above and below it behaves as expected. By creating a dead zone between the two controls - the touch device's noise shouldn't cause your character's position to flicker.

If you wanted you could use this dead zone area as a special ability button

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this