如何让敌人面对一个角色

时间:2013-08-20 06:23:57

标签: 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);
        }


    }

}

2 个答案:

答案 0 :(得分:5)

我注意到您的Character中有一个Class1变量。它被设置为new Character() 但是之后没有其他任何事情可以用它来完成。我猜这意味着您的实际 Character位于您Game的某个地方,此Character 中的另一个Class1是完全不同的变量。很自然地,使用它的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);

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

有一些“懒惰”方法,如单身人士和其他static相关方法,但我不推荐这些方法。

答案 1 :(得分:0)

你说“Class1”是你的敌人......

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

将敌人类中的角色设置为没有意义,我认为你没有在你的敌人类中引用正确的角色实例。

似乎你的敌人类中的角色变量永远不会更新,这可能是它始终在(0,0)的原因