Jump to content
  • Advertisement
Sign in to follow this  
locoflop

XNA Touch Movement

This topic is 2300 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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.


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

}


Thanks, happy coding.

Share this post


Link to post
Share on other sites
Advertisement
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:

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

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


Try something along the lines of:

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

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


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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!