c#xna如何将对象数组复制为不同的数组?

时间:2013-04-13 09:10:14

标签: c# arrays xna copy

我正试图从一个对象数组中拖放。首先,它们将复制到另一个数组中。但是,当我尝试更新原始数组时,副本也有更新。 在这里我的代码:

    namespace drop_test2
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        InputManager input;

        Texture2D texure;
        DragableObject[] dragObjects, copyDragObjects;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            this.IsMouseVisible = true;
            this.input = new InputManager(this);

            this.dragObjects = new DragableObject[6 * 6];
        }
        protected override void LoadContent()
        {
            this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
            this.input.Load();
            this.texure = new Texture2D(this.GraphicsDevice, 50, 50, false, SurfaceFormat.Color);
            Color[] c = new Color[this.texure.Width * this.texure.Height];
            for (int i = 0; i < c.Length; i++) c[i] = Color.White;
            this.texure.SetData(c);

            for (int x = 0; x < 6; x++)
            {
                for (int y = 0; y < 6; y++)
                {
                    var obj = new DragableObject();
                    obj.Size = new Vector2(50);
                    obj.Position = new Vector2(35) + new Vector2(x, y) * obj.Size;
                    this.dragObjects[x + y * 6] = obj;
                }
            }
            this.copyDragObjects = this.dragObjects;
        }
        protected override void Update(GameTime gameTime)
        {
            this.input.Update(gameTime);

            for (int i = 0; i < this.dragObjects.Length; i++)
            {
                if (this.dragObjects[i].CheckHover(input.CurrentCursorPost))
                {
                    this.copyDragObjects[i].Position += new Vector2(.5f, 0);
                }
            }
        }
        Color c = Color.Blue;
        protected override void Draw(GameTime gameTime)
        {
            this.GraphicsDevice.Clear(Color.CornflowerBlue);
            this.spriteBatch.Begin();
            for (int x = 0; x < 6; x++)
            {
                for (int y = 0; y < 6; y++)
                {
                    if ((x + y) % 2 == 0)
                        c = Color.DarkGreen;
                    else
                        c = Color.LightGreen;
                    if (this.dragObjects[x + y * 6].IsHover)
                        c = Color.DarkOrchid * .5f;
                    if (this.dragObjects[x + y * 6].IsSelected)
                        c = Color.DarkRed * .75f;
                    this.spriteBatch.Draw(this.texure, this.dragObjects[x + y * 6].Position, null, c,
                        0f, new Vector2(texure.Width, texure.Height) * .5f, 1f, SpriteEffects.None, 0);
                }
            }
            this.spriteBatch.End();
        }
    }
}

这是dragableobject类

namespace drop_test2
{
    struct DragableObject
    {
        public Vector2 Position
        {
            get;
            set;
        }
        public Vector2 Size
        {
            get;
            set;
        }
        public bool IsSelected
        {
            get;
            set;
        }
        public bool IsHover
        {
            get;
            set;
        }

        public bool CheckHover(Vector2 vector2)
        {
            Rectangle r = new Rectangle((int)(this.Position.X - this.Size.X * .5), (int)(this.Position.Y - this.Size.Y * .5f),
                (int)this.Size.X, (int)this.Size.Y);
            if (r.Contains((int)vector2.X,(int)vector2.Y))
            {
                this.IsHover = true;
                return true;
            }
            this.IsHover = false;
            return false;
        }
    }
}

有谁想帮我解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果使用CopyTo() c#internal指向原始数组。如果要创建新实例,则必须使用Clone()

示例:

object[] src = { "a", "b" };
var dest = src.Clone() as object[];

src[0] = "c";
//// dest still contains "a","b"

另见:Difference between the System.Array.CopyTo() and System.Array.Clone()