まず、スタックオーバーフローは実際にはチュートリアルサイトではありませんが、私はあなたの答えに抵抗することはできません。
OKコードには正直なところでいくつかの問題があります。しかし、まず、あなたの質問に。 TextBoxを使用する代わりに、ラベルを使用します。テキストボックスは、ユーザによって変更される可能性があります。これは問題の1つに私をもたらします。
まず、コントロールをデータのリポジトリとして使用することは、本当に悪いことです。あなたは変数health
で適切なアイデアを持っています。
秒。 Visual Studioの設定でOption Strictをオンにします。そこにいる間、Explicitがオンで、Compareがバイナリで、Inferがオフであることを確認してください。
はthis Stack Overflow answer
を見ては、これらのオプションを変更すると、少ないバグのあるコードを書くことを意味します持っていますが、下側に、あなたはもう少しを記述する必要があります。
最後に、変数とオブジェクトに意味のある名前を選ぶのに少し時間がかかりますが、それが何をするのかを覚えやすくなります。例えば、Timer2
のようなものをTmrGameRunning
と呼んでください。TmrGR
のようなものではありません。 :-)
LblHealth
というラベルを作成する必要があります。私は、TxtBox
コントロールは、単にタイマのダニをカウントするために破棄することができると仮定しています。あなたはそれを必要としません。また、タイマーコントロールとしてタイマーを追加したと仮定すると、タイマーのプロパティでは、ちょうど3秒間のミリ秒数である3000に間隔を設定します。
変更されたコードと説明を参照してください
Public Class Form1
Dim health As Integer
' This will be the variable that note if your player is alive or dead .. True if alive, False if dead
Dim PlayerAlive As Boolean = True
'This is slightly different to your code. In VB, there is an event that will fire when the
'visibility of a textbox changes. The following method will execute when this happens. Just like code
'that you would write when you're handling a button.click event
Private Sub PicBoxEnemy_VisibleChanged(sender As Object, e As EventArgs) Handles PicBoxEnemy.VisibleChanged
If PicBoxEnemy.Visible = True Then
Timer2.Enabled = True
Else
Timer2.Enabled = False
End If
End Sub
'This is a modified version of your timer tick - Don't forget to change the timer .Interval property
'to 3000
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
health = health - 5
'This will change the text of the label to whatever your player's health is and the line below
'will force the label to update
LblHealth.Text = health.ToString
LblHealth.Update()
'Also while the timer is ticking the method below will check the health of your player and decide if
'they are dead or not. If they are, the timer is disabled and the PlayerDead method is called.
AliveOrDead()
End Sub
Private Sub AliveOrDead()
If health <= 0 Then
Timer2.Enabled = False
PlayerDead()
End If
End Sub
'This will be the method that executes when the player is dead. You'll need to add your own code
'for this of course, depending on what you want to do.
Private Sub PlayerDead()
'code here for what happens at the end of the game
End Sub
End Class
ヒント。ゲームを開始するには、ボタンコントロールとButton.Clickイベントハンドラメソッドが必要です(ピクチャボックスが表示されているときにこのタイマーを停止するのを忘れないでください)表示されます)、最後に画像をクリックして非表示にしたときに呼び出されるイベントハンドラ(健康を低下させるタイマーを停止)