【问题标题】:XNA Windows Phone 7 Sprite movementXNA Windows Phone 7 Sprite 运动
【发布时间】:2011-11-20 11:04:30
【问题描述】:

我正在开发一款 Windows 手机游戏,但我在精灵移动方面遇到了困难。 我想要做的是让精灵逐渐移动到屏幕上触摸的位置,当只有一个快速触摸和释放时。 在这一刻,我所能做的就是让精灵立即跳转到触摸位置,或者在按住触摸时移动到触摸位置。

跳转到触摸位置的代码:

TouchCollection touchCollection = TouchPanel.GetState();
        foreach (TouchLocation tl in touchCollection)
        {
            if ((tl.State == TouchLocationState.Pressed)
                 || (tl.State == TouchLocationState.Moved))
            {
                Vector2 newPos = new Vector2(tl.Position.X,tl.Position.Y);

                    if (position != newPos)
                    {
                        while (position.X < newPos.X)
                        {
                            position.X += (float)theGameTime.ElapsedGameTime.Milliseconds / 10.0f * spriteDirectionRight;
                        }
                    }
            }
        }

按住触摸时逐渐移动的代码:

TouchCollection touchCollection = TouchPanel.GetState();
        foreach (TouchLocation tl in touchCollection)
        {
            if ((tl.State == TouchLocationState.Pressed)
                 || (tl.State == TouchLocationState.Moved))
            {
                Vector2 newPos = new Vector2(tl.Position.X,tl.Position.Y);

                    if (position != newPos)
                    {

                            position.X += (float)theGameTime.ElapsedGameTime.Milliseconds / 10.0f * spriteDirectionRight;

                    }
            }
         }

这些在 Sprite 类的 Update() 方法中。

【问题讨论】:

    标签: xna 2d sprite windows-phone-7.1


    【解决方案1】:

    类似这样的东西...对不起,我没有通过编译器运行它,所以可能存在一些语法错误。将这些声明为类中的字段:

    Vector2 newPos;
    bool moving = false;
    

    然后在Update方法中:

            TouchCollection touchCollection = TouchPanel.GetState();
            foreach (TouchLocation tl in touchCollection)
            {
                if ((tl.State == TouchLocationState.Pressed)
                     || (tl.State == TouchLocationState.Moved)
                     || (tl.State == TouchLocationState.Released))
                {
                    newPos = new Vector2(tl.Position.X, tl.Position.Y);
                    moving = true;
                }
            }
            if (moving && newPos != position)
            {
                Vector2 delta = newPos - position;
                Vector2 norm = delta;
                norm.Normalize();
                Vector2 distanceToMove = norm * ((float)gameTime.ElapsedGameTime.TotalMilliseconds * .5f);
                if (distanceToMove.LengthSquared() > delta.LengthSquared())
                {
                    position = newPos;
                    moving = false;
                }
                else
                {
                    position += distanceToMove;
                }
            }
    

    【讨论】:

    • “运算符'*'不能应用于'void'和'float'类型的操作数”它给了我这个Vector2 distanceToMove = delta.Normalize() * (float)theGameTime.ElapsedGameTime的错误。毫秒 * 10f;"
    • 好吧抱歉我修好了,忘记了 Normalize 修改了现有的 Vector2 并且不返回一个。跑的时候又发现了一个bug,这个已经测试过了。
    猜你喜欢
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多