Visual C#对象数组更改数组中的所有元素,而不仅仅是一个 [英] Visual C# object array changes all elements in array instead of just one

查看:175
本文介绍了Visual C#对象数组更改数组中的所有元素,而不仅仅是一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,所以我正在做一个Snake游戏来学习.我创建了蛇的一个对象,其中包含一个称为件的对象数组.因此,碎片组成了蛇.我试图将第一块的x值更改为1,以便将第一块的一个单位向右移动.问题是,当我将第一块的x值更改为1时,第二块的x值也更改为1.因此,即使我只增加了第一块的x,myPiece [1] .x = 6和myPiece [2] .x = 6一个人的价值.如何做到这一点,这样我就可以更改每个单件的价值,而又不改变其其余部分的价值?

I'm new to C# so I'm making a Snake Game to learn. I created an object of a snake which contains an array of objects called pieces. So the pieces make up the snake. I'm trying to change the first piece's x value by 1 in order move the first piece one unit to the right. The problem is that when I change the first piece's x value by 1, the second piece's x value also changes by 1. So myPiece[1].x = 6 and myPiece[2].x = 6 even though I only increased the first one's value. How can I make it so I can change the value of each individual piece without it changing the rest of the piece's values?

class theSnake
{
    private int size;
    private thePiece[] myPieces=new thePiece[50];
    private int xDim;
    private int yDim;


    public theSnake(int pxDim,int pyDim)
    {
        size = 1;
        xDim = pxDim;
        yDim = pyDim;
        for (int i = 1; i <= 49; i++)
        {
           myPieces[i] = new thePiece();
        }

        myPieces[1].setX(xDim/2);  //sets the starting of the snake in the center of the field
        myPieces[1].setY(yDim/2);
        myPieces[1].setDir(3);
    }

    public void move()
    {

         //the pieces trade positions with the positions in front of it
        //the pieces travel backwards instead of forwards so there is no gap created
        for (int i = size; i >= 2; i--)
        {
            myPieces[i] = myPieces[i-1];
        }

        //moves leading snake based on direction given
        switch (myPieces[1].getDir())
        {
            case 1:
                myPieces[1].setY(myPieces[1].getY() + 1);
                break;
            case 2:
                myPieces[1].setY(myPieces[1].getY() - 1);
                break;
            case 3:
                myPieces[1].setX(myPieces[1].getX() + 1);
                break;
            case 4:
                myPieces[1].setX(myPieces[1].getX() - 1);
                break;
        }

    }


class thePiece
{
    private int x;
    private int y;
    private int direction;
    //north = 1 south = 2 east = 3 west =4
    public thePiece()
    {
        x = 0;
        y = 0;
        direction = 1;
    }

}

推荐答案

for (int i = size; i >= 2; i--) 
{ 
    myPieces[i] = myPieces[i-1]; 
} 

因为myPieces[2]指向myPieces[1],换句话说,myPieces[2]myPieces[1]是同一对象.现在,myPieces[3]引用了原始的myPieces[2]对象.

because myPieces[2] point to myPieces[1], in an other word, myPieces[2] and myPieces[1] are the same object. the orignal myPieces[2] object now is referenced by myPieces[3].

如果要分配值,可以使用以下代码

if you want to assign values, you can use following code

 for (int i = size; i >= 2; i--) 
    { 
        myPieces[i].x = myPieces[i-1].x; 
        myPieces[i].y = myPieces[i-1].y; 
        myPieces[i].direction = myPieces[i-1].direction; 
    } 

这篇关于Visual C#对象数组更改数组中的所有元素,而不仅仅是一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆