locked
Subroutine called via screen button does not work as expected RRS feed

  • Question

  • I am building a game program which uses screen buttons to operate via 'on mouse down' sub.  This calls a Spinrolls sub which works as part of the start programming, but runs strange when called via the Spin button.  What am I doing wrong.

    Simplified part of the program :-

    Initialise graphics window
    GraphicsWindow.Hide()
    gw = 800
    gh = 600
    GraphicsWindow.CanResize = "False"
    GraphicsWindow.Top = (Desktop.Height-gh)/2
    GraphicsWindow.Left = (Desktop.Width-gw)/2
    GraphicsWindow.Title = "Fruit Machine 2"
    GraphicsWindow.Width = gw
    GraphicsWindow.Height = gh
    GraphicsWindow.BackgroundColor = "LightBlue"
    Rand1 = 0
    i = 0
    'Show window
    GraphicsWindow.Show()

    'Reduce f0r menu
    gw = gw-200
    GraphicsWindow.MouseDown = OnMouseDown

    delay1=500

      'Draw vertical line and menu
      GraphicsWindow.PenColor = "Black"
      GraphicsWindow.DrawLine(gw,0,gw,gh)
     
      GraphicsWindow.BrushColor = "Black"
      GraphicsWindow.DrawBoundText(gw+15,580,50,"RESPIN")
      GraphicsWindow.DrawBoundText(gw+115,580,50,"QUIT")'Label
     
     Spinrolls()
     
     
        Sub Spinrolls
          
          Rand1 = 0
          i = 0
      For i = 0 To 10  'Rand1
        
        GraphicsWindow.BrushColor = "Red"
        GraphicsWindow.DrawBoundText(gw+115,220,70,"Rand1")
        GraphicsWindow.DrawText(gw+115,240,Rand1)
         Rand1 = Rand1+1
        GraphicsWindow.DrawBoundText(gw+115,320,70,"i")
        GraphicsWindow.DrawText(gw+115,340,i)  
     
     Program.Delay(delay1)
     If i < 10 Then
       'Clear contents of i and Rand1 until i=10
       GraphicsWindow.BrushColor = "LightBlue"
       GraphicsWindow.FillRectangle(gw+115,240,70,20)
       GraphicsWindow.FillRectangle(gw+115,340,70,20)
       EndIf
      EndFor
    EndSub
     

     
      Sub OnMouseDown

       xm = GraphicsWindow.MouseX
       ym = GraphicsWindow.MouseY
     
        'Respin button
        If (xm > gw+15 And xm < gw+85) Then
          If (ym > 580 And ym < 595) Then
            Spinrolls()
          'Add marker 'start'
         ' GraphicsWindow.BrushColor = "Blue"
         ' GraphicsWindow.DrawBoundText(gw+115,50,70,"Start1")
         'Rand1 = 0
          'i = 0
          'delay1=500 ' delay timer
         'Stop1 = 0
          'Spinrolls()
          
          
          EndIf
        EndIf
      'Stop button  
        If (xm > gw+115 And xm < gw+185) Then
          If (ym > 580 And ym < 595) Then
          iend = "True"
          EndIf   
            If (iend) Then
            Program.End()
            EndIf
        EndIf
     
    EndSub

    Monday, September 14, 2015 8:31 PM

Answers

  • Hi steve

    The bug in your program is a fundamental Structure problem.

    You are calling 'Spinrolls() from inside an event handler (a special type of Sub) (Sub OnMouseDown).

    These event handlers are asynchronous and run independently to the Main Program. They are invoked by a program independent event like a mouseDown, buttonClick etc. The main program runs in the UI thread which is usually the best place to do all your processing and especially screen draws.

    Usually best to use event handlers to set flags like mouseDown = "True"

    Then pick these flags up in the Main Loop.

    Initialise() includes GraphicsWindow.MouseDown = OnMouseDown
    
    While "True"
      If mouseDown Then
        'Call Spinrolls() here
        mouseDown = "False"
      EndIf 
      
      Program.Delay(15)
    EndWhile
    
    Sub OnMouseDown
      mouseDown = "True"
    EndSub

    Here's a patch fix with comments to your program: http://smallbasic.com/program/?LZL364



    Tuesday, September 15, 2015 5:17 AM
  • There's a reference guide pdf in the Small Basic directory - good read.

    The Monthly challenges for diverse practice - it's not a book but a very good resource.

    The curriculum and some books here.

    Check out litdevs website - lots of samples and links to his articles: here

    My favourite learning articles: here and hereAnd of coarse - searching the internet.

    btw check out the Shapes object for your program above.

    Litdev extension set for more functionality.

    SB is simple, small, supported, extensible, "powerful" and capable of producing large programs especially using stuff like LDCalls etc. Lots of fun.

    Tuesday, September 15, 2015 11:14 PM

All replies

  • Hi steve

    The bug in your program is a fundamental Structure problem.

    You are calling 'Spinrolls() from inside an event handler (a special type of Sub) (Sub OnMouseDown).

    These event handlers are asynchronous and run independently to the Main Program. They are invoked by a program independent event like a mouseDown, buttonClick etc. The main program runs in the UI thread which is usually the best place to do all your processing and especially screen draws.

    Usually best to use event handlers to set flags like mouseDown = "True"

    Then pick these flags up in the Main Loop.

    Initialise() includes GraphicsWindow.MouseDown = OnMouseDown
    
    While "True"
      If mouseDown Then
        'Call Spinrolls() here
        mouseDown = "False"
      EndIf 
      
      Program.Delay(15)
    EndWhile
    
    Sub OnMouseDown
      mouseDown = "True"
    EndSub

    Here's a patch fix with comments to your program: http://smallbasic.com/program/?LZL364



    Tuesday, September 15, 2015 5:17 AM
  • Many thanks for your guidance and fix.  I have used Visual Basic, but relatively new to Small Basic. Can you recommend a book to learn more. Steve
    Tuesday, September 15, 2015 6:19 PM
  • There's a reference guide pdf in the Small Basic directory - good read.

    The Monthly challenges for diverse practice - it's not a book but a very good resource.

    The curriculum and some books here.

    Check out litdevs website - lots of samples and links to his articles: here

    My favourite learning articles: here and hereAnd of coarse - searching the internet.

    btw check out the Shapes object for your program above.

    Litdev extension set for more functionality.

    SB is simple, small, supported, extensible, "powerful" and capable of producing large programs especially using stuff like LDCalls etc. Lots of fun.

    Tuesday, September 15, 2015 11:14 PM