繁体   English   中英

如何测量Windows Phone上拖动的时间?

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

我想测量拖曳的时间。 从开始(当玩家触摸屏幕时)到结束(当玩家将手指从屏幕上释放时)。 另外,我想测量阻力的距离以计算阻力的速度。

但是我总是收到以下两个错误消息。

1)当我触摸屏幕时,在foreach循环的第一行中收到以下错误消息:

GestureSample gs = TouchPanel.ReadGesture();

Microsoft.Xna.Framework.Input.Touch.ni.dll中发生了类型为'System.InvalidOperationException'的异常,但未在用户代码中进行处理。如果存在用于此异常的处理程序,则可以安全地继续执行该程序。

2)第二条错误消息在此行中:

DragTime = (float)(EndTime - StartTime);

无法将类型“ System.DateTime”转换为“ float”

怎么了? 我该如何解决错误消息?

这是我的代码:

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);
    }
}

如果我是您,则可以这样做-当用户第一次按下屏幕时,请将全局变量TimeSpan timeTimeSpan.Zero 然后,在每次更新时,将gameTime.ElapsedRealTime添加到时间变量。 当用户发布时,您可以从时间变量中获取时间,这很容易。 要获得秒数,只需使用: time.totalSeconds

我已经对您的代码做了一些修改,您可以在这里:

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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