c# - 如何让敌人面对角色

标签 c# xna

嘿伙计们,我遇到了这个问题,我无法让我的敌人转向我的角色 我已经尝试了好几天并四处打听,但一无所获,所以如果你能给我一些想法,那就太棒了。

这是我的敌人类现在在这段代码中一切正常它做我想做的但是它面对鼠标而不是我的角色

class Class1
    {
         Character character = new Character();
         EnemyShip blah = new EnemyShip();

        Texture2D texture;
        Rectangle rectangle;

        public Vector2 origin;
        public Vector2 velocity;
        public Vector2 position;
        float rotation;
        const float forwardvelocity = 1f;
        float friction = 0.1f;

        public Vector2 distance;


        public void LoadContent(ContentManager Content)
        {
            texture = Content.Load<Texture2D>("Ships/WarShip");
            position = new Vector2(800, 300);
        }




        public void Update(GameTime gameTime)
        {
            MouseState mouse = Mouse.GetState();

            distance.X = mouse.X - position.X; //  these two line are the one i want to 
            distance.Y = mouse.Y - position.Y; // change however when i change mouse.X to //say character.Position.X my enemy ship moves towards the top left corner of the screen //and not the character

            rotation = (float)Math.Atan2(distance.Y, distance.X);

            position = velocity + position;

            velocity.X = (float)Math.Cos(rotation) * forwardvelocity;
            velocity.Y = (float)Math.Sin(rotation) * forwardvelocity;

            rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
            origin = new Vector2(rectangle.Width / 2, rectangle.Height / 2);
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Draw(texture, position, null, Color.White, rotation, origin, 1f, SpriteEffects.None, 0);
        }
    }
    }

这是我的角色类

class Character
    {
        public Texture2D texture;
        public float angle = 0;
        public Vector2 velocity;
        public Vector2 Position = new Vector2(0, 0);
        public float forwardvelocity = 5;
        public float friction = 0.03f;
        public Vector2 origin;

        public Rectangle sourcerectangle;

        public void LoadContent(ContentManager Content)
        {
            texture = Content.Load<Texture2D>("Ships/charactership");

        }

        public void Update(GameTime gameTime)
        {
            Position += velocity;
            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                velocity.X = (float)Math.Cos(angle ) * forwardvelocity;
                velocity.Y = (float)Math.Sin(angle) * forwardvelocity;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
                velocity.X = -(float)Math.Cos(angle) * forwardvelocity;
                velocity.Y = -(float)Math.Sin(angle) * forwardvelocity;
            }
                if (Keyboard.GetState().IsKeyDown(Keys.A)) angle -= 0.05f;

                if (Keyboard.GetState().IsKeyDown(Keys.D)) angle += 0.05f;

                else if (velocity != Vector2.Zero)
                {
                    float i = velocity.X;
                    float j = velocity.Y;

                    velocity.X = i -= friction * i;
                    velocity.Y = j -= friction * j;
                }
            //--------------------------------------------------------------

        }



        public void Draw(SpriteBatch spriteBatch)
        {

             sourcerectangle = new Rectangle(0, 0, texture.Width, texture.Height);
            origin = new Vector2(texture.Width / 2, texture.Height / 2);

            spriteBatch.Draw(texture, Position, sourcerectangle, Color.White, angle, origin, 1.0f, SpriteEffects.None, 0);
        }


    }

}

最佳答案

我注意到您的 Class1 中有一个 Character 变量。它被设置为 new Character() 但之后就再也没有用它做过其他事情了。我猜这意味着你的 actual Character 在你的 Game 某处,而另一个 CharacterClass1 完全是一个完全不同的变量。所以很自然地,使用它的 Position 是没有意义的。

因为你的敌人依赖Character变量来进行它自己的计算,传入依赖项:

public void Update(Character c, GameTime gameTime)
{
    MouseState mouse = Mouse.GetState();
    distance.X = c.Position.X - position.X;
    distance.Y = c.Position.Y - position.Y;
...

然后在顶层 Game 或者什么不是:

//your actual character
Character c;
...
//in Game.Update
c.Update(gameTime);
c1.Update(c, gameTime);

然后你可以简单地删除 Class1 中的 Character character = new Character();,因为它没用。

有一些“更懒惰”的方法,例如单例和其他staticness 相关的方法,但我不推荐这些。

关于c# - 如何让敌人面对角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18328282/

相关文章:

3d - XNA:我有一个 1*1*1 的盒子,想知道我从哪一边点击它。 (我的世界克隆版)

c# - 在 ASP.NET 中使用 Ext JS

c# - 如何从 mp4、wmv、flv、mov 视频中获取视频时长

c# - 在 Owin 中间件中覆盖响应主体

c# - 将嵌套 XML 转换为 3d 数组

c# - 每次调用 Equals 都会返回 false,即使值相同 (C#)

c# - 为 XNA C# 游戏制作带有补丁/更新的单文件可视化可定制安装程序

c# - 如何动态隐藏/禁用 WPF 功能区选项卡?

c# - 在 C# 中调整大的奇数比例图像大小

c# - XNA - 保存 xml 文件时出现 UnauthorizedAccessException