0
我的工作是基于地图方格的一个小游戏。我有一个类(clsGrid),它为每个网格广场存储一些属性。网格方形对象被组织成一个列表(clsGrid)。
循环和流读取器正在成功读取文本文件中的属性,将属性放入网格对象中,并将网格对象添加到我的网格列表中。当从网格列表中检索一个网格时,我收到了不寻常的结果。不管我给列表中的索引,我似乎总是得到列表中的最后索引网格。调试器似乎表明,正确的数字都被读入流读取器,它们被添加到gridHolder。然而,在最后的消息框会一直显示我的最后grid.id,无论我给它的索引。VB.net列表不返回索引对象
我一直在研究这个问题,这可能很愚蠢。先谢谢您的帮助。
'A subroutine that generates a map (list of grids)
Sub GenerateMap()
Dim reader As StreamReader = File.OpenText("map1.txt")
Dim gridHolder As New clsGrid
'The streamreader peeks at the map file. If there's nothing in it, a warning is displayed.
If reader.Peek = CInt(reader.EndOfStream) Then
MessageBox.Show("The map file is corrupted or missing data.")
reader.Close()
Else
'If the map file has information, X and Y counts are read
intXCount = CInt(reader.ReadLine)
intYCount = CInt(reader.ReadLine)
'Reads in grid properties until the end of the file
Do Until reader.Peek = CInt(reader.EndOfStream)
gridHolder.TerrainType = CInt(reader.ReadLine)
gridHolder.MovementCost = CInt(reader.ReadLine)
gridHolder.DefensiveBonus = CInt(reader.ReadLine)
gridHolder.ID = listMap.Count
listMap.Add(gridHolder)
Loop
reader.Close()
End If
End Sub
'This function returns a Grid object given an X and Y coordinate
Function lookupGrid(ByVal intX As Integer, ByVal intY As Integer) As clsGrid
Dim I As Integer
Dim gridHolder As New clsGrid
'This formula finds the index number of the grid based on its x and y position
I = ((intX * intYCount) + intY)
gridHolder = listMap.Item(I)
MessageBox.Show(gridHolder.ID.ToString)
Return gridHolder
End Function