在 VB.net 中为网格创建变量

发布于 2024-12-03 15:47:07 字数 254 浏览 2 评论 0原文

我需要将元素存储在多维数组中。具体来说,我需要将“图块”保存到网格中(例如 x:2 y:5 处的草、x:3 y:5 处的泥土等)。使用多维感觉非常混乱并且非常出问题(必须调整我的数组的大小,并且必须创建新的数组,如果它们不存在)。有没有为此制作某种元素?我可以说 obj.getPos(2,5) 并获取我的草元素并使用 obj.setPos(DirtObj, 3, 5) 将其设置为我的泥土元素?

我只是想知道 vb.net 中是否有比多维数组更容易使用的东西,仅此而已。谢谢!

I need to store elements in a multidimensional array. Specifically, I need to save "tiles" to a grid (e.g. Grass at x:2 y:5, Dirt at x:3 y:5, etc.). Using a multidimensional feels very hacked up and is very glitchy (having to resize my arrays and having to create new ones were they are non-existant). Is there some kind of element made for this? Something I can say obj.getPos(2,5) and get my Grass Element and use obj.setPos(DirtObj, 3, 5) to set it to my Dirt Element?

I'm just wondering if there is something easier to use than multidimensional arrays in vb.net, that is all. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

(り薆情海 2024-12-10 15:47:07

选项 1 - 类

如果您要添加、删除和插入对象,我会使用列表列表,因为这将使您可以直接访问给定坐标处的对象( X、Y),让您直接设置对象,而无需重新调整它们的大小。

例如,您可以有一个 Tile 类并使用如下所示的列表:

Dim level As New List(Of List(Of Tile))

' load your level into the lists here!

level(2)(5) ' returns the Tile object at coordinate (2, 5) from above

level(3)(5) = New Tile(TileTypes.Dirt) ' sets a dirt tile at coordinate (3, 5) from above TileTypes would be a simple enum


Option 2 - Enum

如果您使用对象的全部目的只是它们的值,您甚至不需要创建 Tile 类,而只需创建一个 TileTypes 枚举,其中包含一些值,例如 < code>Dirt、Grass 等并设置它们:

Public Enum TileTypes
    Dirt
    Grass
    'etc
End Enum

Dim level As New List(Of List(Of TileTypes))

' load your level into the lists here!

level(2)(5) ' returns the TileTypes value stored at coordinate (2, 5) from above

level(3)(5) = TileTypes.Dirt ' sets a dirt tile at coordinate (3, 5) from above

您应该能够在此基础上进行构建并从那里获取它。

Option 1 - Class

If you're going to be adding, removing and inserting objects, I would use a List of Lists since this will give you direct access to the object at a given coordinate (X, Y) and let you set the object directly without having to re-size them.

For example, you could have a Tile class and use the Lists like this:

Dim level As New List(Of List(Of Tile))

' load your level into the lists here!

level(2)(5) ' returns the Tile object at coordinate (2, 5) from above

level(3)(5) = New Tile(TileTypes.Dirt) ' sets a dirt tile at coordinate (3, 5) from above TileTypes would be a simple enum


Option 2 - Enum

If all you're using the objects for is their value you don't even need to create a Tile class instead you could just create a TileTypes enum with some values like Dirt, Grass, etc and set them:

Public Enum TileTypes
    Dirt
    Grass
    'etc
End Enum

Dim level As New List(Of List(Of TileTypes))

' load your level into the lists here!

level(2)(5) ' returns the TileTypes value stored at coordinate (2, 5) from above

level(3)(5) = TileTypes.Dirt ' sets a dirt tile at coordinate (3, 5) from above

You should be able to build upon this and take it from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文