简体   繁体   中英

How can I measure the time of a drag on my Windows Phone?

I want to measure the time of a drag. From the beginning(when the player touches the screen) to the end(when the player releases his finger from the screen). In addition, I want to measure the distance of the drag to calculate the velocity of the drag.

But I always get the following two error messages.

1)When I touche the screen I get this error message in the first line of the foreach loop:

GestureSample gs = TouchPanel.ReadGesture();

An exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.Input.Touch.ni.dll but was not handled in user code If there is a handler for this exception, the program may be safely continued.

2)The second error message is in this line:

DragTime = (float)(EndTime - StartTime);

Cannot convert type 'System.DateTime' to 'float'

What is wrong? What can I do to fix the error messages?

Here is my code:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Vector2 DragVelocity;
    TimeSpan StartTime;
    DateTime EndTime;
    float DragTime;
    Vector2 StartPoint, EndPoint, DragDistance;
    bool FirstTimePressed = true;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        TargetElapsedTime = TimeSpan.FromTicks(333333);
        InactiveSleepTime = TimeSpan.FromSeconds(1);
    }

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        TouchPanel.EnabledGestures = GestureType.FreeDrag;
    }

    protected override void Update(GameTime gameTime)
    {
        TouchCollection touchCollection = TouchPanel.GetState();
        foreach (TouchLocation tl in touchCollection)
        {
            GestureSample gs = TouchPanel.ReadGesture();
            if (FirstTimePressed == true)
            {
              if ((tl.State == TouchLocationState.Pressed))
              {
                StartTime = gs.Timestamp;
                StartPoint = gs.Delta;
                FirstTimePressed = false;
              }
            }
            if ((tl.State == TouchLocationState.Released))
            {
                EndTime = DateTime.Now;
                DragTime = (float)(EndTime - StartTime);
                EndPoint = gs.Delta;
                DragDistance = EndPoint - StartPoint;
                DragVelocity = DragDistance / DragTime;
                FirstTimePressed = true;
            }
        }

        while (TouchPanel.IsGestureAvailable)
        {
            GestureSample gs = TouchPanel.ReadGesture();
            switch (gs.GestureType)
            {
                case GestureType.FreeDrag:

                break;
            }
        }
        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        base.Draw(gameTime);
    }
}

If I were you I would do it this way- when user presses the screen for the first time, set a global variable TimeSpan time to TimeSpan.Zero . Then, on every update, add gameTime.ElapsedRealTime to time variable. When user releases, your can get your time from time variable, it's easy. To get seconds just use: time.totalSeconds .

I've modified your code a little, here you go:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    float DragVelocity, DragDistance, DragTime;
    TimeSpan time;
    Vector2 StartPoint, EndPoint;
    bool isThisFirstTime = true;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        TargetElapsedTime = TimeSpan.FromTicks(333333);
        InactiveSleepTime = TimeSpan.FromSeconds(1);
    }

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        TouchPanel.EnabledGestures = GestureType.DragComplete | GestureType.FreeDrag;
    }

    protected override void Update(GameTime gameTime)
    {
        while (TouchPanel.IsGestureAvailable)
        {
            GestureSample gs = TouchPanel.ReadGesture();
            switch (gs.GestureType)
            {
                case GestureType.FreeDrag:
                    if (isThisFirstTime == true)
                    {
                        time = TimeSpan.Zero;
                        StartPoint = gs.Position;
                        isThisFirstTime = false;
                    }
                    else
                    {
                        EndPoint = gs.Position;
                        time += gameTime.ElapsedGameTime;
                    }
                    break;

                case GestureType.DragComplete:
                    isThisFirstTime = true;
                    DragTime = (float) time.TotalSeconds;
                    DragDistance = Vector2.Distance(StartPoint, EndPoint);
                    DragVelocity = DragDistance / DragTime;
                    break;
            }
        }
        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        base.Draw(gameTime);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM