オブジェクトの配列からドラッグ アンド ドロップしようとしています。最初は、別の配列にコピーされます。しかし、元の配列を更新しようとすると、コピーも更新されます。ここに私のコード:
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();
}
}
}
これはドラッグ可能なオブジェクト クラスです
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;
}
}
}
この問題を解決するために私を助けたい人はいますか?