How long has the player tapped on the iOS/Android display screen? How can I measure the time?

Kim Strasser 1,036 Reputation points
2024-07-22T14:08:15.7066667+00:00

In my game the player needs to tap on the iOS/Android display screen to make a gameplay action. I want to find out how long the player pressed his finger against the display screen. For example, if the player pressed his finger 1 second on the screen than I could make a different or stronger action when the tap gesture is finished. I thought maybe I could multiply the time of the tap gesture with another value to determine the strength of a gameplay action in my game.

For example: Strength of gameplay action = xx seconds * 1000

But I don´t know how to measure the time that the finger was on the screen. How can I measure the time of the tap gesture?

  TouchPanel.EnabledGestures = GestureType.Tap | GestureType.FreeDrag | GestureType.DragComplete;

  while (TouchPanel.IsGestureAvailable)
  {
      GestureSample gs = TouchPanel.ReadGesture();
      switch (gs.GestureType)
      {
          case GestureType.Tap:
          //  Strength of gameplay action = xx seconds * 1000  ?

          break;
          case GestureType.FreeDrag:

          break;
          case GestureType.DragComplete:

          break;
     }
 }
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,031 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,369 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Kim Strasser 1,036 Reputation points
    2024-07-28T12:39:31.9333333+00:00

    I use the following code to get the touch began and touch end:

    DateTime TapCurrentTimeUTC;
    DateTime TapReleasedTimeUTC;
    TimeSpan TapRemainingTimeUTC = TimeSpan.Zero;
    
    protected override void LoadContent()       
    {             
        _spriteBatch = new SpriteBatch(GraphicsDevice);  
        TouchPanel.EnabledGestures = GestureType.Tap | GestureType.FreeDrag | GestureType.DragComplete;          
    }          
    
    protected override void Update(GameTime gameTime)        
    {              
          Touchcollection = TouchPanel.GetState();
          if (Touchcollection.Count > 0)
          {
              if (Touchcollection[0].State == TouchLocationState.Released)
              {
                  TapReleasedTimeUTC = DateTime.UtcNow;
                  TapRemainingTimeUTC = TapReleasedTimeUTC - TapCurrentTimeUTC;                   
              }
              
              if (Touchcollection[0].State == TouchLocationState.Pressed)
              {
                  TapCurrentTimeUTC = DateTime.UtcNow;
                  TapRemainingTimeUTC = TimeSpan.Zero;            
              }   
          }
    
        base.Update(gameTime);        
    } 
    
    while (TouchPanel.IsGestureAvailable) 
    { 
        GestureSample gs = TouchPanel.ReadGesture(); 
        switch (gs.GestureType) 
        { 
            case GestureType.Tap: 
                // Then I use 
            break; 
            case GestureType.FreeDrag: 
    
            break; 
            case GestureType.DragComplete: 
    
            break; 
        } 
    }
    
    

    When I tap on the display screen the Touchcollection State is TouchLocationState.Pressed and when I don´t touch the display anymore then the Touchcollection State is TouchLocationState.Released.

    TapRemainingTimeUTC is the duration of the tap on the display screen.

    Afterwards case GestureType.Tap is executed and I use TapRemainingTimeUTC there.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.