第二敌人对象不像第一个敌人

时间:2015-06-25 12:29:55

标签: c# xna

为什么当我将一个新的Enemy对象添加到我要加载到游戏中的对象列表中时,第二个不会像第一个那样在屏幕上移动?

ObjList代码

class Items
{
    public static List<Obj> objList = new List<Obj>();

    public static void Initialize()
    {
        objList.Add(new Player(new Vector2(50, 50)));
        objList.Add(new Enemy(new Vector2(500, 500)));
        objList.Add(new Enemy(new Vector2(600, 200)));
        objList.Add(new BlueBall(new Vector2(300, 400)));
        objList.Add(new GreenBall(new Vector2(350, 100)));
        objList.Add(new OrangeBall(new Vector2(900, 250)));
        objList.Add(new PinkBall(new Vector2(100, 500)));
        objList.Add(new RedBall(new Vector2(600, 500)));
        objList.Add(new YellowBall(new Vector2(500, 250)));

    }

列表中的第三个条目是我希望加载到游戏中的第二个敌人。第一个可以正常工作,但第二个敌人不会像第一个一样在屏幕上移动。下面是带有移动代码的Enemy类。

敌人等级

class Enemy : Obj
{
    float spd = 1;
    float detectionDistance = 175;

    public Enemy(Vector2 pos)
        : base(pos)
    {
        position = pos;
        spriteName = "BlackBall";
        speed = spd;
    }

    public override void Update()
    {
        rotation = point_direction(position.X, position.Y, Player.player.position.X, Player.player.position.Y);
        speed = Math.Abs(Player.player.position.X - position.X) < 175 && Math.Abs(Player.player.position.Y - position.Y) < 230 ? spd : 0;

        base.Update();
    }



    public override void pushTo(float pix, float dir)
    {
        float newX = (float)Math.Cos(MathHelper.ToRadians(dir));
        float newY = (float)Math.Sin(MathHelper.ToRadians(dir));
        newX *= pix;
        newY *= pix;
        if (!Collision(new Vector2(newX, newY), new Player(Vector2.Zero)))
        {
            base.pushTo(pix, dir);
        }
    }

    //Uses Trig to calculate the shortest distance to the player then moves towards that position
    private float point_direction(float x, float y, float x2, float y2)
    {
        float diffx = x - x2;
        float diffy = y - y2;
        float adj = diffx;
        float opp = diffy;
        float tan = opp / adj;
        float res = MathHelper.ToDegrees((float)Math.Atan2(opp, adj));
        res = (res - 180) % 360;
        if (res < 0) { res += 360; }
        return res;
    }
}

线speed = Math.Abs(Player.player.position.X - position.X) < 175 && Math.Abs(Player.player.position.Y - position.Y) < 230 ? spd : 0;是敌人应该做的事情,如果他们在半径175范围内移向玩家,但这似乎不适用于我的第二个敌人,我看不到这是为什么。

然后在Game1.cs中更新对象

protected override void Update(GameTime gameTime)
    {
        foreach (Obj o in Items.objList)
        {
            o.Update();
        }

对象类

class Obj : Microsoft.Xna.Framework.Game
{
    public Vector2 position;
    public float rotation = 0.0f;
    public Texture2D spriteIndex;
    public string spriteName;
    public float speed = 0.0f;
    public float scale = 1.0f;
    public bool alive = true;
    public Rectangle area;
    public bool solid = false;

    public int score;

    public Obj(Vector2 pos)
    {
        position = pos;
    }

    private Obj()
    {

    }


    public virtual void Update()
    {
            if (!alive) return;

            UpdateArea();
            pushTo(speed, rotation);
        }




    public virtual void LoadContent(ContentManager content)
    {
        spriteIndex = content.Load<Texture2D>("sprites\\" + spriteName);
        area = new Rectangle((int)position.X - (spriteIndex.Width / 2), (int)position.Y - (spriteIndex.Height / 2), spriteIndex.Width, spriteIndex.Height);
    }


    public virtual void Draw(SpriteBatch spriteBatch)
    {

        if (!alive) return;

        Rectangle Size;
        Vector2 center = new Vector2(spriteIndex.Width / 2, spriteIndex.Height / 2);
        spriteBatch.Draw(spriteIndex, position, null, Color.White, MathHelper.ToRadians(rotation), center, scale, SpriteEffects.None, 0);

    }

    public bool Collision(Vector2 pos, Obj obj)
    {
        Rectangle newArea = new Rectangle(area.X, area.Y, area.Width, area.Height);
        newArea.X += (int)pos.X;
        newArea.Y += (int)pos.Y;

        foreach (Obj o in Items.objList)
        {
            if (o.GetType() == obj.GetType() && o.solid)
                if (o.area.Intersects(newArea))
                    return true;
        }
        return false;
    }

    public Obj Collision(Obj obj)
    {
        foreach (Obj o in Items.objList)
        {
            if (o.GetType() == obj.GetType())
                if (o.area.Intersects(area))
                    return o;
        }
        return new Obj();
    }

    public void UpdateArea()
    {
        area.X = (int)position.X - (spriteIndex.Width / 2);
        area.Y = (int)position.Y - (spriteIndex.Height / 2);
    }

    public T CheckCollisionAgainst<T>() where T : Obj
    {
        // If collision detected, returns the colliding object; otherwise null.
        return Items.objList
            .OfType<T>()
            .FirstOrDefault(o => o.area.Intersects(area));
    }

    public virtual void pushTo(float pix, float dir)
    {
        float newX = (float)Math.Cos(MathHelper.ToRadians(dir));
        float newY = (float)Math.Sin(MathHelper.ToRadians(dir));
        position.X += pix * (float)newX;
        position.Y += pix * (float)newY;
    }
}

0 个答案:

没有答案