这个二维数组初始化代码有什么问题? [英] What's wrong with this 2D array initializing code?
问题描述
我正在尝试获取游戏关卡单元格的位置并将它们映射到二维数组.我想这样做是为了让每个地面单元格(而不是背景单元格)与我的玩家角色相撞.
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);
}
}
可以在这里找到:如何使用锯齿状数组来记录这些瓷砖的 x 和 y 轴?以及对我正在尝试做的事情的更好描述.
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.
所以,当我运行这段代码时,我在 tilePositions 中得到一个空数组.嗯,x 和 y 值在那里,但值都是 0.这些值应该是单元格的位置数据.
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.
tilePosition 数组如下所示:
Here is what the tilesPosition array looks like:
不过,我仍在处理碰撞代码……我需要先解决这个问题,然后才能弄清楚那部分.
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.
推荐答案
如果你这样做了
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.)
这篇关于这个二维数组初始化代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!