【问题标题】:c#, xna, wp7 touch screen not workingc#, xna, wp7 触摸屏不工作
【发布时间】:2011-05-02 18:42:22
【问题描述】:

我正在制作一个 wp7 应用程序,但我的触摸屏似乎无法正常工作。我写的代码如下

protected override void Update(GameTime gameTime)
{
    TouchCollection touches = TouchPanel.GetState();
    if (touches.Count > 0 && touches[0].State == TouchLocationState.Pressed)
    {
        SpriteFont font = Content.Load<SpriteFont>("Font");
        Point touchPoint = new Point((int)touches[0].Position.X, (int)touches[0].Position.Y);
        spriteBatch.Begin();
        spriteBatch.DrawString(font, "hello ", new Vector2(touchPoint.X, touchPoint.Y), Color.White);
        spriteBatch.End();

        while (TouchPanel.IsGestureAvailable)
        {
            GestureSample gesture = TouchPanel.ReadGesture();
            switch (gesture.GestureType)
            {
                case GestureType.Tap:
                case GestureType.DoubleTap:
                    spriteBatch.Begin();
                    spriteBatch.DrawString(font, " ", new Vector2(touchPoint.X, touchPoint.Y), Color.White);
                    spriteBatch.End();
                    break;
            }
        }
    }
    base.Update(gameTime);
}

【问题讨论】:

  • 这怎么行不通?有没有接触过?第一个是什么状态?是否抛出任何错误?

标签: c# windows-phone-7 touchscreen xna-4.0


【解决方案1】:

问题可能是您试图在 Update 方法中绘制一些东西。 Draw 方法用于绘图。 因此,在您的情况下,您在 Update 方法中绘制一些东西,然后在 Draw 方法中绘制背景,这样您就覆盖了您的文本。

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    // overriding here
    ... draw background

    // *draw your text should be here
    base.Draw(gameTime);
}

所以解决方法是移动“spriteBatch.DrawString(...);”绘制方法并将其放在下面 在您的代码中添加背景或在绘图方法中添加深度参数。

【讨论】:

    【解决方案2】:

    现在查看您的代码,似乎没有任何问题,但是您是否尝试过单步执行?我看到点击或双击做同样的事情,但也许有些东西被挂断了。

    不过,我首先想到的是您的 XAML。你有控制权或其他东西吗?这可能会捕捉到你的手势。

    如果两者都不起作用,您能否发布更多信息?

    【讨论】:

    • 这是一个 XNA 问题,不是 Silverlight,所以不会有任何 XAML
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多