2014-12-24 2 views
-1

Я должен создать игру змей и лестниц для проекта. У меня есть 30 мест на моей доске. Если маркер проходит мимо 30 сбои приложений с:Змеи и лестницы

«Необработанное исключение типа„System.NullReferenceException“произошло в appCounter.exe

Дополнительная информация: Ссылка на объект не указывает на экземпляр объекта.»

Я хочу Marker, чтобы остаться там, где она есть, если число проката доведет его прошлым 30.

здесь мой код:

Public Class frmBoard 
Dim intScore As Integer 

Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click 
    'declartions 
    Dim intValue As Integer 
    Dim strCounterName As String 
    Dim roll As New Random() 
    Dim intRoll = roll.Next(1, 7) 

    'make the current counter visible 
    If intScore > 0 Then 
     strCounterName = "lblCounter" & intScore 
     Me.Controls(strCounterName).Visible = False 
    End If 


    'input 
    intValue = intRoll 
    intScore = intScore + intValue 
    strCounterName = "lblcounter" & intScore.ToString 

    'output 
    txtMove.Text = intRoll 
    Me.Controls(strCounterName).Visible = True 

    If intScore = 3 Then 
     lblCounter3.Visible = False 
     lblCounter22.Visible = True 
     intScore = 22 
    End If 

    If intScore = 5 Then 
     lblCounter5.Visible = False 
     lblCounter8.Visible = True 
     intScore = 8 
    End If 

    If intScore = 11 Then 
     lblCounter11.Visible = False 
     lblCounter26.Visible = True 
     intScore = 26 
    End If 

    If intScore = 20 Then 
     lblCounter20.Visible = False 
     lblCounter29.Visible = True 
     intScore = 29 
    End If 

    If intScore = 17 Then 
     lblCounter17.Visible = False 
     lblCounter4.Visible = True 
     intScore = 4 
    End If 

    If intScore = 19 Then 
     lblCounter19.Visible = False 
     lblCounter7.Visible = True 
     intScore = 7 
    End If 

    If intScore = 21 Then 
     lblCounter21.Visible = False 
     lblCounter9.Visible = True 
     intScore = 9 
    End If 

    If intScore = 27 Then 
     lblCounter27.Visible = False 
     lblCounter1.Visible = True 
     intScore = 1 
    End If 

    If intScore = 31 Then 
     lblCounter31.Visible = False 
     lblCounter1.Visible = True 
     intScore = 1 
    End If 

    If intScore = 32 Then 
     lblCounter32.Visible = False 
     lblCounter1.Visible = True 
     intScore = 1 
    End If 

    If intScore = 33 Then 
     lblCounter33.Visible = False 
     lblCounter1.Visible = True 
     intScore = 1 
    End If 

    If intScore = 30 Then 
     MsgBox("Winner") 
    End If 

    If intScore > 30 Then 
     MsgBox("OvErBoArD") 
    End If 
    End Sub 


End Class 
+0

Заметим, что 'vba' не то же самое, как' vb.net'. –

+0

Вам нужно будет добавить какую-то проверку, когда вы добавите «бросок кубика» пользователя, чтобы увидеть «Если roll + intScore> 30» Затем «оставьте счетчик один», «Еще добавьте рулон в счетчик». – Chrismas007

+0

Я новичок в этом, и мой учитель не вникает в объяснение, извините Bjorn –

ответ

0

Это покажет пользователю, что они перешли и пропустили добавление броска к общей сумме, чтобы оставить их на месте.

Изменение:

'input 
intValue = intRoll 
intScore = intScore + intValue 
strCounterName = "lblcounter" & intScore.ToString 

To:

'input 
intValue = intRoll 
If intScore + intValue > 30 Then 
    MsgBox "OvErBoArD" 
Else 
    intScore = intScore + intValue 
End If 
strCounterName = "lblcounter" & intScore.ToString 
+0

Спасибо! Это сработало отлично! –

+0

Mark как принятый тогда пожалуйста. – Chrismas007

-2

Поскольку существует только 30 счетчиков, когда intScore идет более 30 он выкинет это исключение.

Добавить инструкцию Если ваш вклад следующим образом:

'input 
intValue = intRoll 
If intscore + intvalue < 30 then 
intscore=intscore + intvalue 
End if 

strCounterName = "lblcounter" & intScore.ToString 

Таким образом, он не будет идти за тридцать, прежде чем он устанавливает lblcounter и не ошибка из

+0

Это не соответствует критериям пользователя «Я хочу, чтобы маркер оставался там, где он есть, если число показов приведет к тому, что оно пройдет 30». – Chrismas007

Смежные вопросы