不应该超出范围的数组索引?

时间:2017-03-14 04:28:04

标签: arrays vb.net

所以我试图为我正在努力开发的视频游戏创建自己的项目生成器,一切都有工作,遇到了许多我能够解决的问题,除了这个我是无法解决。这里的问题是数组我必须计算特定于该类型的项目的数量,当它不应该时。我不确定代码的其余部分,但它可以很好地遍历武器部分,它会崩溃"并在装甲部分创建错误。

这方面的背景是我有4个不同的班级,5种不同的武器,5种不同的物品类型以及不同稀有度下每个班级的一定数量的物品。 弹出这个错误的地方是Armor的常见部分,它会通过武器。因此,对于公共场所我设置的每50个项目生成它进入下一个类,即从archer开始,生成50个archer类项目,然后程序切换到下一个类。

 ItemType = Armor(aArray) 'Item type, this is where the error happens

 If aCount = (DcCount / 4) Then          'This is the counter to determine the item type
       aArray = aArray + 1               'the class for the weapon, i.e. wizard
 ElseIf aCount = ((DcCount / 4) * 2) Then
       aArray = aArray + 1
 ElseIf aCount = ((DcCount / 4) * 3) Then
       aArray = aArray + 1
 End If

这里是武器部门确定武器类的代码:

                ItemType = Weapons(wArray)

                If wCount = (DcCount / 4) Then
                    wArray = wArray + 1
                ElseIf wCount = ((DcCount / 4) * 2) Then
                    wArray = aArray + 1
                ElseIf wCount = ((DcCount / 4) * 3) Then
                    wArray = wArray + 1
                End If

正如你所看到的那样。

装甲部分发生的错误是当aCount达到50时,由于某种原因它会增加到阵列外部。而不是0 - > 1它变为0 - > 4,我根本不明白它为什么要迭代到外面

我会发布我的整个源代码,但它太大了,这里是代码的关键点的偶然结合:

  1. 不同的项目数组和计数,wCounter和wArray等在每次增加项目级别后重置为0。 wCount,aCount等用于从1到最大项目的循环,具体取决于项目的稀有度。

        For Level = 1 To 101
    
        'Resets the counters for each level by default
        cCounter = 0
        uCounter = 0
        rCounter = 0
        urCounter = 0
        lCounter = 0
        wCount = 1
        hCount = 1
        aCount = 1
        lCount = 1
        bCount = 1
        wArray = 0
        aArray = 0
        hArray = 0
        lArray = 0
        bArray = 0
    
  2. 每个项目都遵循完全相同的算法生成,只是根据项目类型使用不同的变量。我将展示武器和盔甲:

          For wCount = 1 To DcCount
    
                'Generates the weapon stats
                ItemType = Weapons(wArray)
                itemDamage = Rnd((50 * Level)) + ((5 * Level))
                itemClass = cClass(wArray)
                itemName = ItemType
    
                addItems.Items.Add(New ListViewItem(New String() {ItemCount.ToString, ItemType, itemName, itemDamage.ToString, itemClass, Rarity(0).ToString}))
    
                'Creates item text file
                TextName = "Item" & ItemCount & "." & ItemType & ".Level" & Level & "." & itemClass & "." & Rarity(0)
                Dim path As String = "C:\Users\" & Username & "\Desktop\Realms\Items\" & TextName & ".txt"
                'Appends the stats to text file
                Using fw = File.CreateText(path)
                    fw.WriteLine("Name: " & itemName)
                    fw.WriteLine("Type: " & ItemType)
                    fw.WriteLine("Damage: " & itemDamage)
                    fw.WriteLine("Class: " & itemClass)
                    fw.WriteLine("Rarity: " & Rarity(0))
                End Using
    
                'increments counters
                ItemCount = ItemCount + 1
                cCounter = cCounter + 1
    
                If wCount = (DcCount / 4) Then
                    wArray = wArray + 1
                ElseIf wCount = ((DcCount / 4) * 2) Then
                    wArray = aArray + 1
                ElseIf wCount = ((DcCount / 4) * 3) Then
                    wArray = wArray + 1
                End If
    
            Next
    
    
            'Generates Armor
            For aCount = 1 To DcCount
    
                'Generates the armor stats
                ItemType = Armor(aArray)
                itemArmor = Rnd((10 * Level)) + ((2 * Level))
                itemClass = cClass(aArray)
                itemName = ItemType
                addItems.Items.Add(New ListViewItem(New String() {ItemCount.ToString, ItemType, itemName, itemDamage.ToString, itemClass, Rarity(0).ToString}))
    
                'Creates item text file
                TextName = "Item" & ItemCount & "." & ItemType & ".Level" & Level & "." & itemClass & "." & Rarity(0)
                Dim path As String = "C:\Users\" & Username & "\Desktop\Realms\Items\" & TextName & ".txt"
    
                'Appends the stats to the text file
                Using fw = File.CreateText(path)
                    fw.WriteLine("Name: " & itemName)
                    fw.WriteLine("Type: " & ItemType)
                    fw.WriteLine("Damage: " & itemDamage)
                    fw.WriteLine("Class: " & itemClass)
                    fw.WriteLine("Rarity: " & Rarity(0))
                End Using
    
                'Increments counters
                ItemCount = ItemCount + 1
                cCounter = cCounter + 1
    
                If aCount = (DcCount / 4) Then
                    aArray = aArray + 1
                ElseIf aCount = ((DcCount / 4) * 2) Then
                    aArray = aArray + 1
                ElseIf aCount = ((DcCount / 4) * 3) Then
                    aArray = aArray + 1
                End If
    
    
            Next
    
  3. 编辑:手头的问题似乎丢失了,这里有清晰度:

    默认情况下,Item类型数组设置为0,max设置为3.当它创建移动到下一个项目所需的项目数量时,aArray = 1,它将移动到aArray = 4,这是索引之外,我不知道它为什么这样做。 到第51项aArray应该是1,而不是4。

    但是现在它似乎也在wArray中发生了......我不知道为什么它在不应该的范围之外迭代到范围之外。

0 个答案:

没有答案