XNA敌人徘徊

时间:2014-03-25 20:56:49

标签: c# xna

任何人都可以帮助我。我希望我的精灵只能左右移动/上下移动。然后,如果玩家在X / Y的特定范围内靠近敌人,则敌人将跟随玩家直到玩家逃离特定范围。 (此代码只能跟随X坐标中的玩家)

class Enemy
{

        Texture2D enemy_Texture;
        Vector2 enemy_Position;
        Vector2 enemy_Origin;
        Vector2 enemy_Velocity;
        Rectangle enemy_Rectangle;
        float enemy_Rotation = 0f;

        float enemy_DistanceFromPlayer;
        float enemy_OldDistanceFromPlayer;
        bool right = true;

        public Enemy(Texture2D texture, Vector2 position, float distance)
        {
            enemy_Texture = texture;
            enemy_Position = position;
            enemy_DistanceFromPlayer = distance;
            enemy_OldDistanceFromPlayer = enemy_DistanceFromPlayer;
        }
        float player_Position;

        public void Update(Player player)
        {
            enemy_Position += enemy_Velocity;
            enemy_Origin = new Vector2(enemy_Texture.Width / 2, enemy_Texture.Height / 2);

            player_Position = player.X - enemy_Position.X;

            if (player_position<enemy_DistanceFromPlayer)
            {
                enemy_Position += new Vector2(1, 0);
            }
            if (player_position > enemy_DistanceFromPlayer)
            {
                enemy_Position += new Vector2(-1, 0);
            }



            /*
            if (enemy_DistanceFromPlayer <= enemy_OldDistanceFromPlayer)
            {
                right = true;
                enemy_Velocity.X = 1f;

            }
            else if (enemy_DistanceFromPlayer >= enemy_OldDistanceFromPlayer)
            {
                right = false;
                enemy_Velocity.X = -1f; 
            }
            //if (right) enemy_DistanceFromPlayer += 1; enemy_DistanceFromPlayer -= 1;


            if (player_Position >= -20 && player_Position <= 20)
            {
                if (player_Position < -1)
                {
                    enemy_Velocity.X = 1f;
                }
                else if (player_Position > 1)
                {
                    enemy_Velocity.X = -1f;
                }
                else if (player_Position == 0)
                {
                    enemy_Velocity.X = 0f;
                }
            }*/


        }

        public void Draw(SpriteBatch spritebatch)
        {
            if (enemy_Velocity.X > 0)
            {
                spritebatch.Draw(enemy_Texture, enemy_Position, null, Color.Wheat, enemy_Rotation, enemy_Origin, 1f,SpriteEffects.FlipHorizontally, 0f);
            }
            else
            {
                spritebatch.Draw(enemy_Texture, enemy_Position, null, Color.Wheat, enemy_Rotation, enemy_Origin, 1f, SpriteEffects.None, 0f);
            }
        }
}

3 个答案:

答案 0 :(得分:2)

更简单的方法是远距离而不是独立处理X / Y.在类别级别字段的某处定义chase_threshold。

const float chase_threshold = 4f;  // Chase player if less than 4 squares away

然后这可能是你的追逐逻辑:

float distance = Vector2.Distance(player, enemy);
if (distance < chase_threshold)
{
    Vector2 direction;

    // Should we go left/right or up/down?
    if (Math.Abs(player.X - enemy_Position.X) > Math.Abs(player.Y - enemy_Position.Y)
        direction = new Vector2(player.X - enemy_Position.X, 0);
    else
        direction = new Vector2(0, player.Y - enemy_Position.Y);

    enemy_Position += direction * enemy_Velocity;
}

答案 1 :(得分:0)

现在这是一个有效的代码,但精灵一直跟着我。我想知道如何让它停下来。

类敌人     {

    Texture2D enemy_Texture;
    Vector2 enemy_Position;
    Vector2 enemy_Origin;
    Vector2 enemy_Velocity;
    Rectangle enemy_Rectangle;
    float enemy_Rotation = 0f;
    float enemy_DistanceFromPlayer;
    float enemy_OldDistanceFromPlayer;
    bool right = true;

    public Enemy(Texture2D texture, Vector2 position, float distance)
    {
        enemy_Texture = texture;
        enemy_Position = position;
        enemy_DistanceFromPlayer = distance;
        enemy_OldDistanceFromPlayer = enemy_DistanceFromPlayer;
    }



    Vector2 player_Position;
    Vector2 distanceFromTarget;
    float rangeToMove = 4f; // this is the range where the enemy will start to move, if the player get near it
    float distanceFromPlayer;
    public void Update(Player player, GameTime gameTime)
    {

        player_Position = new Vector2(player.X, player.Y);
        distanceFromTarget = player_Position - enemy_Position;
        distanceFromPlayer = Vector2.Distance(player_Position, enemy_Position);
        distanceFromTarget.Normalize();
        if (rangeToMove<distanceFromPlayer)
        {
            enemy_Position += distanceFromTarget * (float)gameTime.ElapsedGameTime.TotalMilliseconds * .1f;
        }



    }

答案 2 :(得分:0)

我假设你的.1f;指的是敌人追逐玩家的速度。 如果是这样,如果你添加一个速度变量,你的.1f就是你的敌人。

尝试添加到您的

float speed;

if (rangeToMove<distanceFromPlayer)
{

enemy_Position += distanceFromTarget * (float)gameTime.ElapsedGameTime.TotalMilliseconds    * speed;

}
else
{
speed = 0.0f;
}