C#XNA - 收集图像后处理它们

时间:2015-06-23 12:03:35

标签: c# memory-management xna drawing

我已经遇到了同样的问题了一段时间,一旦玩家从游戏中收集了一个对象,它就会被删除,但游戏仍然认为它存在并抛出:

  

System.Drawing.dll中出现未处理的“System.ComponentModel.Win32Exception”类型异常

     

附加信息:操作成功完成

我已经阅读了一些其他帖子,他们似乎在说你应该使用Dispose()删除图片后收集它们以释放一些内存但我不确定这是否是正确的解决方案对我来说,我也不知道如何做到这一点。

上面的警告发生在我的Object类中(它特别突出显示了行public Obj(Vector2 pos),这可以在下面看到。

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;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试一些事情:

protected override void Dispose(bool disposing)
{
    spriteIndex.Dispose() 
    // any other object to dispose
    base.Dispose(disposing);
}