What's wrong with this 2D array initializing code?

醉酒当歌 提交于 2020-01-06 07:54:16

问题


I am trying to take the positions of the cells of a game level and map them to a 2D array. I want to do this so I can make each ground cell (and NOT background cell) collidable with my player character.

Below is the current code that someone created for me:

        int tileSize = 20;
        int screenSizeInTiles = 30;

        // initializing multidimensional array of points
        var tilePositions = new System.Drawing.Point[screenSizeInTiles, screenSizeInTiles];

        for (int x = 0; x < screenSizeInTiles; x++)
        {
            for (int y = 0; y < screenSizeInTiles; y++)
            {
                tilePositions[x, y] = new System.Drawing.Point(x * tileSize, y * tileSize);

            }
        }

It can be found here: How can I use a jagged array to record the x and y axes of these tiles? along with a better description of what I'm trying to do.

So, when I run this code, I get an empty array in tilePositions. Well, the x, and y values are there, but the values are all 0. The values should be the position data for the cells.

Here is what the tilesPosition array looks like:

http://imgur.com/VYyxp

I'm still working on the collision code though... I need this to work before I can figure that part out.

Thank you all incredibly much, you have been so helpful! I am still a beginner, but am working around the clock to make myself a better programmer.


回答1:


if you did

        int tileSize = 20;
        int screenSizeInTiles = 30;

        // initializing jagged array of points
        var tilePositions = new Point[screenSizeInTiles][screenSizeInTiles];

        for (int x = 0; x < screenSizeInTiles; x++)
        {
            for (int y = 0; y < screenSizeInTiles; y++)
            {
                tilePositions[x][y] = new Point(x * tileSize, y * tileSize);
            }
        }

it would be jagged (an array of arrays.)



来源:https://stackoverflow.com/questions/13859191/whats-wrong-with-this-2d-array-initializing-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!