开发者

Creating a variable for a grid in VB.net

开发者 https://www.devze.com 2023-04-02 18:29 出处:网络
I need to store elements in a multidimensional arra开发者_如何转开发y.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 ver

I need to store elements in a multidimensional arra开发者_如何转开发y. 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消