Reputation: 1
Iv'e been creating a game on VB for a school assignment and I am having trouble creating a difficulty setting. I have four buttons on my title screen in which you can choose a difficulty. when you click one of the buttons it's supposed to add to the "speed" variable on the main game. I've tried a few methods and it still doesn't seem to work.
Here's the code that I am using.
Main game variables:
Public speed As Single = 5
Dim xVel As Single = Math.Cos(speed) * speed
Dim yVel As Single = Math.Sin(speed) * speed
Title screen code (difficulty select):
Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
gameMain.Show()
If btnEasy.Enabled = False Then
gameMain.speed = 5
End If
If btnMedium.Enabled = False Then
gameMain.speed = gameMain.speed + 2
End If
If btnHard.Enabled = False Then
gameMain.speed = gameMain.speed + 5
End If
If btnInsane.Enabled = False Then
gameMain.speed = gameMain.speed + 10
End If
End Sub
So when you click a button it gets disabled and changes color. when you start the game, if one of the buttons is disabled its supposed to add the corresponding amount to the "speed" variable.
I've also tried putting similar code on the Main game form and that also didn't work. I'm trying to keep the code as simple as possible as I am still an amateur at VB.
Any answers would be appreciated.
Upvotes: 0
Views: 104
Reputation: 603
As suggested by @AYK I'm posting my comment above as an answer so we can close out this question....
Its a bit of a hack but can you move the speed variable into a global module, make it a global static variable and see if that works?
Upvotes: 0
Reputation: 942255
Your code only changes the "speed" field, it doesn't re-calculate the values for xVel and yVel.
You solve this problem by making speed a property instead of a field. The property setter can adjust the velocity vector:
Private _speed As Single
Private xVel As Single
Private yVel As Single
Private angle As Single
Public Sub New()
InitializeComponent()
Speed = 5
End Sub
Public Property Speed() As Single
Get
Return _speed
End Get
Set(ByVal value As Single)
_speed = value
xVel = _speed * Math.Cos(angle)
yVel = _speed * Math.Sin(angle)
End Set
End Property
You'll need to work on angle
next.
Upvotes: 4