简体   繁体   English

c#xna如何将对象数组复制为其他数组?

[英]c# xna how to copy an object array as different array?

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

Anyone wanna help me to solving this problem? 有人想帮助我解决这个问题吗?

If you use CopyTo() c# internally refers to the original array. 如果使用CopyTo() c#在内部引用原始数组。 If you want to create a new instance you have to use Clone() . 如果要创建新实例,则必须使用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() 另请参见: System.Array.CopyTo()和System.Array.Clone()之间的区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM