如何在C#中将光标导航到我的2D数组中?

时间:2018-07-20 16:30:26

标签: c# arrays unity3d multidimensional-array

因此,自从两天以来,我一直在努力寻找答案,我仍然是一名学生,不知道是否无法理解其他帖子,或者我的案情太具体而无法解决以及我在互联网上找到的任何内容。

正如我在标题中所说的,我有一个称为“ piece”的2D类数组,每当我开始游戏时,它都会创建一个具有随机行和列的2D数组(快速解释一下目标是将所有块连接起来为了赢球,您可以旋转棋子以建立连接)。 OnMouseClick()一切正常。功能,但是由于我需要使用键盘上的箭头进行归档,因此遇到了一些麻烦。

我有一个函数,每次我启动或重置它时都会生成谜题(因此在Start()中调用,而在我按下“清除”按钮时在Update()中调用它)像这样:

public piece[,] pieces;
public GameObject Cursor;

void GeneratePuzzle()
    {
        pieces = new piece[width, height];

        int[] auxValues = { 0, 0, 0, 0 };

        for (int h = 0; h < height; h++)
        {
            for (int w = 0; w < width; w++)
            {
                //width restrictions
                if (w == 0)
                    auxValues[3] = 0;
                else
                    auxValues[3] = pieces[w - 1, h].values[1];

                if (w == width - 1)
                    auxValues[1] = 0;
                else
                    auxValues[1] = Random.Range(0, 2);

                //heigth resctrictions
                if (h == 0)
                    auxValues[2] = 0;
                else
                    auxValues[2] = pieces[w, h - 1].values[0];

                if (h == height - 1)
                    auxValues[0] = 0;
                else
                    auxValues[0] = Random.Range(0, 2);

                //tells piece type
                int valueSum = auxValues[0] + auxValues[1] + auxValues[2] + auxValues[3];

                if (valueSum == 2 && auxValues[0] != auxValues[2])
                    valueSum = 5;

                go = (GameObject)Instantiate(piecesPrefabs[valueSum], new Vector3(origin.position.x + w, origin.position.y, origin.position.z + h), Quaternion.identity);
                go.transform.parent = gameObject.transform;

                while (go.GetComponent<piece>().values[0] != auxValues[0] ||
                      go.GetComponent<piece>().values[1] != auxValues[1] ||
                      go.GetComponent<piece>().values[2] != auxValues[2] ||
                      go.GetComponent<piece>().values[3] != auxValues[3])

                {
                    go.GetComponent<piece>().RotatePiece();
                }
                pieces[w, h] = go.GetComponent<piece>();

                instantiatedPieces.Add(go.gameObject);
            }
        }
    }

到目前为止,我一直在尝试类似的操作以使光标在此数组中移动:

public void Update()
{
        for (int h = 0; h < height; h++)
        {
            if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                h++;
                if (h > height)
                    h = 0;
            }
            for (int w = 0; w < width; w++)
            {
                if (Input.GetKeyDown(KeyCode.UpArrow))
                {
                    w++;
                    if (w > width)
                        w = 0;
                }
                // Cursor.transform.position = pieces[w, h].transform.position; ==> I suppose this is where it should be
            }            
        }
}

但是我最终超出了范围,或者光标是如此之快,以至于看不到它遍及每片。为了清楚起见,我希望能够按行和列将光标移至该2D数组中的每个片段上(我想这是要走的路),之后,我需要从我的光标结束的特定棋子的棋子类,但我想我可以找到它。

英语不是我的母语,很抱歉,如果您需要更多信息来帮助我,我会尽力而为。 非常感谢您的任何帮助,我们将不胜感激!

4 个答案:

答案 0 :(得分:1)

我不确定您要使用代码做什么。 我没有得到您在更新中使用以下行的内容。

     private static TableSchema TableSchema;

     public static void main(String[] args) {

        try {
            TableSchema = TableSchemaReader.read(TableSchemaResource);
        } catch (IOException e) {
            log.error("Table schema can not be read from {}. Process aborted.", TableSchemaResource);
            return;
        }

        DataflowDfpOptions options = PipelineOptionsFactory.fromArgs(args)
                //.withValidation()
                .as(DataflowDfpOptions.class);

        Pipeline pipeline = Pipeline.create(options);

        Stopwatch sw = Stopwatch.createStarted();
        log.info("DFP data transfer from GS to BQ has started.");

        pipeline.apply("ReadFromStorage", TextIO.read()
                .from("gs://my-test/stream/*.gz")
                .withCompression(Compression.GZIP))
                .apply("TransformToTableRow", ParDo.of(new TableRowConverterFn()))
                .apply("WriteToBigQuery", BigQueryIO.writeTableRows()
                        .to(options.getTableId())
                        .withMethod(STREAMING_INSERTS)
                        .withCreateDisposition(CREATE_NEVER)
                        .withWriteDisposition(WRITE_APPEND)
                        .withSchema(TableSchema)); //todo: use withJsonScheme(String json) method instead


        pipeline.run().waitUntilFinish();

        log.info("DFP data transfer from GS to BQ is finished in {} seconds.", sw.elapsed(TimeUnit.SECONDS));
    }

    /**
     * Creates a TableRow from a CSV line
     */
    private static class TableRowConverterFn extends DoFn<String, TableRow> {

        @ProcessElement
        public void processElement(ProcessContext c) throws Exception {

            String[] split = c.element().split(",");

            //Ignore the header line
            //Since this is going to be run in parallel, we can't guarantee that the first line passed to this method will be the header
            if (split[0].equals("Time")) {
                log.info("Skipped header");
                return;
            }

            TableRow row = new TableRow();
            for (int i = 0; i < split.length; i++) {

                //This throws NEP!!!
                TableFieldSchema col = TableSchema.getFields().get(i);

                //String is the most common type, putting it in the first if clause for a little bit optimization.
                if (col.getType().equals("STRING")) {
                    row.set(col.getName(), split[i]);
                } else if (col.getType().equals("INTEGER")) {
                    row.set(col.getName(), Long.valueOf(split[i]));
                } else if (col.getType().equals("BOOLEAN")) {
                    row.set(col.getName(), Boolean.valueOf(split[i]));
                } else if (col.getType().equals("FLOAT")) {
                    row.set(col.getName(), Float.valueOf(split[i]));
                } else {
                    //Simply try to write it as a String if
                    //todo: Consider other BQ data types.
                    row.set(col.getName(), split[i]);
                }
            }
            c.output(row);
        }
    }

尝试删除该行。

答案 1 :(得分:1)

尝试一下:

 public void Update()
 {
    for (int h = 0; h < height; h++)
    {
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            h++;
            if (h > height)
                h = 0;
        }
     }
     for (int w = 0; w < width; w++)
     {
         if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             w++;
             if (w > width)
                 w = 0;
         }
         // Cursor.transform.position = pieces[w, h].transform.position; ==> I suppose this is where it should be
     }            

 }

或者这个:

 public void Update()
 {
    for (int h = 0; h < width; h++)
    {
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            h++;
            if (h > height)
                h = 0;
        }
     }
     for (int w = 0; w < height; w++)
     {
         if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             w++;
             if (w > width)
                 w = 0;
         }
         // Cursor.transform.position = pieces[w, h].transform.position; ==> I suppose this is where it should be
     }            

 }

答案 2 :(得分:1)

对于WindowsForms,我将使用KeyDown事件并增加/减少h或w,并在到达边缘时将它们设置为零或最大值。

答案 3 :(得分:1)

以下是我的评论示例:

如果要在不依赖按键的情况下将光标移动到要更新的数组元素上,则不需要for循环。

 // Declared outside of your update makes these class variables that
 // will live as long as this object exists.

 int w = 0; // May be better to change this to cursorXPos
 int h = 0; // May be better to name this cursorYPos

 public void Update()
 {
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            w--;
            if (w < 0)
                w = width -1;
        }
        else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            w++;
            if (w >= width)
                w = 0;
        }

        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            h--;
            if (h < 0)
                h = height -1;
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            h++;
            if (h >= height)
                h = 0;
        }

        Cursor.transform.position = pieces[w, h].transform.position; 
 }