8 Replies - 686 Views - Last Post: 31 October 2011 - 01:52 PM Rate Topic: -----

#1 hutchy4122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-October 11

Error with rolling a dice: won't work second time round

Posted 31 October 2011 - 11:24 AM

Hi guys, I'm trying to create a game that involves rolling a dice. When I load the program and click the btnRoll button, the dice works fine and stops after a random number (between 10 and 20) of face changes. It then records the score in the a new line in a ListBox. Only problem is, when I press the roll button again, the face only changes once before recording the result.

I have tried creating a separate program that simply rolls the dice without recording the result, and it works fine. So I used roughly the same code in my game, but it still won't work. I have tried looking online for help for a few days now, so I figured this would be a good time to ask here.

Here is the code for 'btnRoll'

    
    Private Sub btnRoll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRoll.Click

        btnRoll.Enabled = False
        tmrTimer.Enabled = True

        intCounter = 0 '<< used to count the number of face changes

        intRollStopped = Rand(10, 20) '<< using the previously declared function "Rand" to generate a number between 10 and 20

    End Sub



And here is the code for my timer 'tmrTimer'

    Private Sub tmrTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick

        intCounter = intCounter + 1

        Randomize()
        intCurrentRoll = Int(Rnd() * 6) + 1 '<< chooses a random number between 1 and 6 for each face change

        Select Case intCurrentRoll '<< changes face according to said random number
            Case 1 : imgDice.BackgroundImage = My.Resources.face1
            Case 2 : imgDice.BackgroundImage = My.Resources.face2
            Case 3 : imgDice.BackgroundImage = My.Resources.face3
            Case 4 : imgDice.BackgroundImage = My.Resources.face4
            Case 5 : imgDice.BackgroundImage = My.Resources.face5
            Case 6 : imgDice.BackgroundImage = My.Resources.face6
        End Select

        Select Case intRollStopped '<< makes blnRollStopped true, therefore stopping timer at selected point (intRollStopped)
            Case 10 : If intCounter >= 10 Then
                    blnRollStopped = True
                    intCounter = 0
                End If
            Case 11 : If intCounter >= 11 Then
                    blnRollStopped = True
                    intCounter = 0
                End If
            Case 12 : If intCounter >= 12 Then
                    blnRollStopped = True
                    intCounter = 0
                End If
            Case 13 : If intCounter >= 13 Then
                    blnRollStopped = True
                    intCounter = 0
                End If
            Case 14 : If intCounter >= 14 Then
                    blnRollStopped = True
                    intCounter = 0
                End If
            Case 15 : If intCounter >= 15 Then
                    blnRollStopped = True
                    intCounter = 0
                End If
            Case 16 : If intCounter >= 16 Then
                    blnRollStopped = True
                    intCounter = 0
                End If
            Case 17 : If intCounter >= 17 Then
                    blnRollStopped = True
                    intCounter = 0
                End If
            Case 18 : If intCounter >= 18 Then
                    blnRollStopped = True
                    intCounter = 0
                End If
            Case 19 : If intCounter >= 19 Then
                    blnRollStopped = True
                    intCounter = 0
                End If
            Case 20 : If intCounter >= 20 Then
                    blnRollStopped = True
                    intCounter = 0
                End If
        End Select

    End Sub



I'd really appreciate any help and advice.

Is This A Good Question/Topic? 0
  • +

Replies To: Error with rolling a dice: won't work second time round

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Error with rolling a dice: won't work second time round

Posted 31 October 2011 - 12:29 PM

Remember that button click event and timer event are two different running threads. We are not sure which of them will reach the condition first. So better set counter to 0 before enabling the timer:
Private Sub btnRoll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRoll.Click

    btnRoll.Enabled = False
    intCounter = 0 '<< used to count the number of face changes

    intRollStopped = Rand(10, 20) '<< using the previously declared function "Rand" to generate a number between 10 and 20
    tmrTimer.Enabled = True
End Sub
I think this will help
Was This Post Helpful? 1
  • +
  • -

#3 hutchy4122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-October 11

Re: Error with rolling a dice: won't work second time round

Posted 31 October 2011 - 12:56 PM

You're right, theoretically and logically this should work, but I'm afraid it doesn't... Thanks for the tip though, could prove useful in later projects :)
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Error with rolling a dice: won't work second time round

Posted 31 October 2011 - 01:15 PM

Also what this variable doing? blnRollStopped? doesnt this need a reset also?
Also I missed the part that you reset the counter in your ifs conditions
Was This Post Helpful? 1
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Error with rolling a dice: won't work second time round

Posted 31 October 2011 - 01:16 PM

Depends on which Timer you are talking about? The one available in standard toolbox uses the GUI thread.

This post has been edited by AdamSpeight2008: 31 October 2011 - 01:19 PM

Was This Post Helpful? 1
  • +
  • -

#6 hutchy4122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-October 11

Re: Error with rolling a dice: won't work second time round

Posted 31 October 2011 - 01:33 PM

Sorry guys, I forgot to include this:

    Private Sub imgDice_BackgroundImageChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles imgDice.BackgroundImageChanged

        If blnRollStopped = True And blnP1Turn = True Then

            blnRollStopped = False
            btnRoll.Enabled = True
            tmrTimer.Enabled = False

            x = intP1Rolls - 1
            ReDim Preserve strP1Rolls(x + 1)
            strP1Rolls(x + 1) = intCurrentRoll.ToString
            ListBox1.Items.Add(strP1Rolls(x + 1))

            blnP1Turn = False

        ElseIf blnRollStopped = True And blnP1Turn = False Then

            blnRollStopped = False
            btnRoll.Enabled = True
            tmrTimer.Enabled = False

            x = intP2Rolls - 1
            ReDim Preserve strP2Rolls(x + 1)
            strP2Rolls(x + 1) = intCurrentRoll.ToString
            ListBox2.Items.Add(strP2Rolls(x + 1))

            blnP1Turn = True

        End If

    End Sub



And smohd, I have written the code so that the counter is reset each time btnRoll is clicked

Adam: tmrTimer is a timer from the standard toolbox
Was This Post Helpful? 0
  • +
  • -

#7 hutchy4122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-October 11

Re: Error with rolling a dice: won't work second time round

Posted 31 October 2011 - 01:40 PM

P.S. imgDice is the PictureBox that is my dice
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Error with rolling a dice: won't work second time round

Posted 31 October 2011 - 01:47 PM

Sure it must be set to false first before starting rolling again. So in your btnRoll_Click event, set it to false.
blnRollStopped = False


And Windows timers component are designed for a single-threaded (as opposed to multithreaded) environment, so as Adams said, uses the same GUI thread(I mixed it somewhere).
Was This Post Helpful? 1
  • +
  • -

#9 hutchy4122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-October 11

Re: Error with rolling a dice: won't work second time round

Posted 31 October 2011 - 01:52 PM

Thanks mate, that problem was really bugging me :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1