【问题标题】:Moving a sprite in the direction it is facing, why does my attempt not work?朝它所面对的方向移动一个精灵,为什么我的尝试不起作用?
【发布时间】:2014-09-03 07:08:15
【问题描述】:

我正在使用 MonoGame 开发 Windows 8 商店应用程序/游戏(不适用于手机)。我也在使用带有 XAML 的项目,但是这个问题与 XAML 无关。

我试图让一艘船朝着它所面对的方向移动,并且可以通过按左右键来改变方向来旋转船。向上键用于将船沿其所面对的方向移动。

游戏开始时,船的图像/纹理最初是朝下的(想象一个朝下的箭头),所以当我按下向上键时,我想将其向下移动,但它向右移动。我收集到这与轮换有关吗?

我已经用谷歌搜索了如何解决我的问题并尝试了各种方法,这是我最好的尝试,但是它不起作用。

我的父精灵类:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ship_Meteor_Game_V1
{
    abstract class cSprite
    {
        #region Properties
        Texture2D spriteTexture;
        Rectangle spriteRectangle;
        Vector2 spritePosition;

        public Texture2D SpriteTexture { get { return spriteTexture; } set { spriteTexture = value; } }
        public Rectangle SpriteRectangle { get { return spriteRectangle; } set { spriteRectangle = value; } }
        public Vector2 SpritePosition { get { return spritePosition; } set { spritePosition = value; } }
        #endregion

        abstract public void Update(GameTime gameTime);

        abstract public void Draw(SpriteBatch spriteBatch);

    }
}

我的玩家等级:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ship_Meteor_Game_V1
{
    class cPlayer : cSprite
    {
        Vector2 origin;
        float rotation;
        float speed;

        public cPlayer()
        {
        }

        public cPlayer(Texture2D newTexture2D, Vector2 newPosition)
        {
            SpriteTexture = newTexture2D;
            SpritePosition = newPosition;
            speed = 2;
            rotation = 0;
        }
        public override void Update(GameTime gameTime)
        {

            if(Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                rotation = rotation + 0.1f;
            }

            if(Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                rotation = rotation - 0.1f;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                Move();
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(SpriteTexture, SpritePosition, null, Color.White, rotation, origin, 0.2f, SpriteEffects.None, 0f);
        }
        public void Move()
        {
            Vector2 direction = new Vector2( (float)Math.Cos(rotation), (float)Math.Sin(rotation));
            direction.Normalize();
            SpritePosition = SpritePosition + (direction * speed);
        }
    }
}

基本上,我希望一艘船向它所面对的方向移动,但它却不断地向它所面对的任何方向侧向移动,我不知道如何解决它。如果我有的话,我可以向你展示你想要的任何额外的类/代码。

PS:有人知道可以接受鼠标和键盘输入的变量/类型吗?

【问题讨论】:

    标签: xna xna-4.0 monogame


    【解决方案1】:

    看不出该代码有任何明显错误。

    猜测是您在内容管理器中使用的船舶图形不是朝上的。

    如果是这种情况,您必须在图像编辑器中旋转它,或者修改您的起始旋转。

    我敢打赌,它会朝右,这是可以理解的混淆,因为在常规数学中,弧度 0 会朝右。然而,在 Xna 中,0 上升了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多