0

I'm trying to make a drag and drop from an array of object. At first, they will copied in another array. But, when i try to updating the original array, the copy has updating too. here my codes:

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

this is the dragableobject class

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

Anyone wanna help me to solving this problem?

1 Answer 1

1

If you use CopyTo() c# internally refers to the original array. If you want to create a new instance you have to use Clone().

Example:

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

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

Also see: Difference between the System.Array.CopyTo() and System.Array.Clone()

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for solving my problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.