【问题标题】:XNA sprite movement gone wrongXNA 精灵运动出错了
【发布时间】:2016-07-04 10:42:57
【问题描述】:

大家好,首先,这是我的精灵的图像:

我在我的游戏中缩小了它,所以它在游戏中并没有那么大。

所以无论如何我想要的下面代码的结果:

protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        spritePos = spriteVelocity + spritePos;
        spriteR = new Rectangle((int)spritePos.X, (int)spritePos.Y, spriteT.Width, spriteT.Height);
        spriteOrigin = new Vector2(spriteR.Width/2, spriteR.Height / 2);

        if (Keyboard.GetState().IsKeyDown(Keys.Right)) rotation += 0.1f;
        if (Keyboard.GetState().IsKeyDown(Keys.Left)) rotation -= 0.1f;

        if (Keyboard.GetState().IsKeyDown(Keys.Up))
        {
            spriteVelocity.X = (float)Math.Cos(rotation) * tangentialVelocity;
            spriteVelocity.Y = (float)Math.Sin(rotation) * tangentialVelocity;
        } else if (spriteVelocity != Vector2.Zero)
        {
            float i = spriteVelocity.X;
            float j = spriteVelocity.Y;

            spriteVelocity.X = i -= friction * i;
            spriteVelocity.Y = j -= friction * j;
        }

        base.Update(gameTime);
    }

然后是Draw方法:

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

        spriteBatch.Begin();

        foreach(Bushes bush in bushes)
        {
            bush.Draw(spriteBatch);
        }

        player.Draw(spriteBatch);
        spriteBatch.Draw(spriteT, spritePos, null, Color.White, rotation, spriteOrigin, 0.1f, SpriteEffects.None, 0f);
        spriteBatch.End();

        base.Draw(gameTime);
    }

当我按下“向上”箭头时,这个小火箭应该朝着它所指向的方向前进(因为我可以用左右箭头旋转它以使其指向所需的位置),但是这段代码的结果是当我按下向上箭头时,精灵首先向右移动,当它旋转时它不会去它指向的地方而是有点侧向:/

我在这里做错了什么?

PS。所有未在代码中声明和初始化的变量都是全局变量,并在 Initialize() 和 LoadContent() 方法中初始化:/

【问题讨论】:

    标签: c# xna 2d


    【解决方案1】:

    如果你的旋转初始化为 0,它应该指向右边,看看下面三角圆上 0 指向的位置:)

    您可以使用 Vector2.Transform 计算您的方向并作为 value 参数(第一个)Vector2(0,1) 给出,这样您的参考方向就会向上。

    或者你向右旋转你的精灵并将你的角度初始化为 Pi/2。

    【讨论】:

      猜你喜欢
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-13
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多