Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XNA-对象不';我不能正确地移动_Xna - Fatal编程技术网

XNA-对象不';我不能正确地移动

XNA-对象不';我不能正确地移动,xna,Xna,好的,我有一个ship对象,用户用箭头键控制它 if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true) mAngle -= 0.1f; else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true) mAngle += 0.1f; if (aCurrentKeyboardState.I

好的,我有一个ship对象,用户用箭头键控制它

        if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
            mAngle -= 0.1f;
        else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
            mAngle += 0.1f;

        if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true)
        {
           // mSpeed.Y = SHIP_SPEED;
           // mDirection.Y = MOVE_UP;

            velocity.X = (float)Math.Cos(mAngle) * SHIP_SPEED;
            velocity.Y = (float)Math.Sin(mAngle) * SHIP_SPEED;
        }
        else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
        {
            mSpeed.Y = SHIP_SPEED;
            mDirection.Y = MOVE_DOWN;
        }
以下两个方法在另一个类中

//Update the Sprite and change it's position based on the passed in speed, direction and elapsed time.
    public void Update(GameTime theGameTime, Vector2 velocity, float theAngle)
    {
        Position += velocity * (float)theGameTime.ElapsedGameTime.TotalSeconds;
        shipRotation = theAngle;
    }

    //Draw the sprite to the screen
    public void Draw(SpriteBatch theSpriteBatch)
    {
        Vector2 Origin = new Vector2(mSpriteTexture.Width / 2, mSpriteTexture.Height / 2);

        theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), 
            Color.White, shipRotation, Origin, Scale, SpriteEffects.None, 0);
    }
问题是:程序认为船的前部是一个侧翼,所以在某种程度上,当我向上按时,它会向一侧移动。我不知道这是为什么


建议?

如果我正确理解您的问题,您可以通过旋转船舶纹理来解决问题。只需在绘制方法中将90或-10添加到shipRotation:

theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), 
        Color.White, shipRotation + 90, Origin, Scale, SpriteEffects.None, 0);


希望有帮助

如果我正确理解您的问题,您只需旋转船舶纹理即可解决问题。只需在绘制方法中将90或-10添加到shipRotation:

theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), 
        Color.White, shipRotation + 90, Origin, Scale, SpriteEffects.None, 0);


希望有帮助

然后更改原始精灵以匹配程序“认为”的飞船前部。

然后更改原始精灵以匹配程序“认为”的飞船前部。

简单,使用以下两行代码:

        velocity.X = (float)Math.Cos(mAngle) * SHIP_SPEED;
        velocity.Y = (float)Math.Sin(mAngle) * SHIP_SPEED;
例如,切换正反方向,并使其中一个为负数:

        velocity.X = -(float)Math.Sin(mAngle) * SHIP_SPEED;
        velocity.Y = (float)Math.Cos(mAngle) * SHIP_SPEED;
如果现在倒转,只需切换负片:

        velocity.X = (float)Math.Sin(mAngle) * SHIP_SPEED;
        velocity.Y = -(float)Math.Cos(mAngle) * SHIP_SPEED;

简单,用以下两条线:

        velocity.X = (float)Math.Cos(mAngle) * SHIP_SPEED;
        velocity.Y = (float)Math.Sin(mAngle) * SHIP_SPEED;
例如,切换正反方向,并使其中一个为负数:

        velocity.X = -(float)Math.Sin(mAngle) * SHIP_SPEED;
        velocity.Y = (float)Math.Cos(mAngle) * SHIP_SPEED;
如果现在倒转,只需切换负片:

        velocity.X = (float)Math.Sin(mAngle) * SHIP_SPEED;
        velocity.Y = -(float)Math.Cos(mAngle) * SHIP_SPEED;

我这样做了,但看起来好像不到90。我想这可能是因为旋转不是用弧度来衡量的。。。我不想解决我的问题,因为它会影响所有其他派生类。我这样做了,但看起来它似乎小于90。我想这可能是因为旋转不是用弧度来衡量的。。。我不想解决我的问题,因为它会影响所有其他派生类。