locked
Subs and Interactivity RRS feed

  • Question

  • Is using a sun the only way to use the event mousedown?

    Example: Sub click, graphicswindow.addrectangle (100, 100)

    graphicswindow.mousedown = (click)

    Is this the only way to do it? My problem is that i want the program to wait until someone has clicked before it continues. When i use a sub, the program skips over that, so it moves ahead befoer the person can click. What im left with is a new section of the program starting while the click event still is there. Also, i am assuming that not usuing a sub would make the program wait....... Correct me if im wrong.

    Thanks


    • Edited by Stewart555 Friday, March 16, 2012 8:37 PM
    Friday, March 16, 2012 8:34 PM

Answers

  • Although your direct approach works as well, however, there is a glitch! Rat() will be executed every time a user clicks on his mouse, non-stop! That is, the event will happen anywhere in your code, not only when you want or need that to occur!
    Although you can turn it off by assigning Graphics.MouseDown= DoNothing sub, once you don't need it anymore.

    I know you're having some difficulties understanding an indirect approach to triggered events. But let's try it again! :D

    I've re-written the code to use 3 separate subs: Rat(), WaitMouseClick() and ClearScreen().

    Rat() is the triggered sub, WaitMouseClick() is responsible to just sitting-duck until user clicks one of the mouse buttons and ClearScreen() tidies up the screen.

    Here's the code; just study it and be not afraid to ask where you don't get it:

    '====================================================================
    GraphicsWindow.MouseDown= Rat    ' Activates MouseClick event
    
    GraphicsWindow.DrawText(10,10, "Please, click at the screen here to continue")
    
    WaitMouseClick()    ' Waits user's mouse click
    
    ClearScreen()       ' Clears GraphicsWindow
    
    ' Just some random code after mouse button event
    GraphicsWindow.DrawRectangle(100,100 200,200)  
    Sound.PlayClickAndWait()
    '====================================================================
    
    ' ---------------------------------------------------------------------------------------------------------------------
    Sub Rat
      ' Sets boolean flag to "True", to indicate a mouse button was clicked.
      
      Clicked= "True"   ' Boolean Flag is SET!!! ;-)
      
    EndSub
    ' ---------------------------------------------------------------------------------------------------------------------
    Sub WaitMouseClick
      ' Resets boolean flag variable to "False"  and awaits until it turns back into "True"
      'through Rat() triggered MouseDown event sub.
      
      Clicked= "False"
      
      While Clicked <> "True"
        Program.Delay(100)
      EndWhile
      
    EndSub
    ' ---------------------------------------------------------------------------------------------------------------------
    Sub ClearScreen
      ' Clears GraphicsWindow w/o erasing Shapes objects.
      
      GraphicsWindow.BrushColor= GraphicsWindow.BackgroundColor
      GraphicsWindow.FillRectangle(0,0, GraphicsWindow.Width, GraphicsWindow.Height)
      
    EndSub
    ' ---------------------------------------------------------------------------------------------------------------------

    • Edited by GoToLoopEditor Saturday, March 17, 2012 6:50 PM
    • Marked as answer by Stewart555 Sunday, March 18, 2012 3:47 PM
    Saturday, March 17, 2012 6:46 PM
    Answerer

All replies

  • The idea would be to put the program in a wait mode until a click is registered.  A delay inside the wait is good to prevent mashing the cpu while it is waiting.

    Example:

    GraphicsWindow.DrawText(50,50,"Click To Continue")
    clicked = 0
    GraphicsWindow.MouseDown = OnMouseDown
    While (clicked <> 1)
      Program.Delay(100)
    EndWhile
    GraphicsWindow.DrawText(50,100,"Click Registered")
    
    Sub OnMouseDown
      clicked = 1
    EndSub
      

    Friday, March 16, 2012 9:06 PM
  • Is using a Sub the only way to use the event mousedown?

    Triggered events demand Subs in Small Basic, and I believe it happens in almost any language based on events!

    But it's easy to deal with it: Create a conditional loop to check a variable in which a Sub is responsible to modify when it is triggered-called.

    Here is a brief example how to do it:

    Clicked= "False" ' Clicked is used as a boolean flag variable w/ either "True" or "False" state!'
    
    GraphicsWindow.MouseDown= Rat ' Sub to call whenever a mouse button is pressed'
    
    GraphicsWindow.DrawText(10,10, "Waiting For Mouse Click...")
    
    While Clicked <> "True" ' Loop runs until Clicked = "True", that is, after a mouse button was pressed!'
      Program.Delay(100) ' A lil' delay to cool our CPUs down!
    EndWhile
    
    GraphicsWindow.DrawText(10,50, "Mouse Button Was Clicked!")
    Sound.PlayClickAndWait()
    
    Sub Rat
      Clicked= "True"   ' Boolean Flag is SET!!! ;-)'
    EndSub

    Hope it helps ya!

    • Edited by GoToLoopEditor Saturday, March 17, 2012 2:18 AM End-of-line errors
    Friday, March 16, 2012 9:16 PM
    Answerer
  • A delay inside the wait is good to prevent mashing the cpu while it is waiting.

     WOW! It is indeed true. I just opened Task Manager to watch my example and it was using all of my CPU!!!

    Thx for the advice, litdev!


    Friday, March 16, 2012 9:34 PM
    Answerer
  • Ok thanks. Thank you to gotoloop who explained what he wrote
    • Proposed as answer by GAniman Saturday, March 17, 2012 6:17 PM
    Saturday, March 17, 2012 2:07 AM
  • gotoloop,

    I couldnt get it to work in my program. :(

    I used it excactly how you did in yours but its still skipping ahead.

    What should i do?

    • Proposed as answer by GAniman Saturday, March 17, 2012 6:17 PM
    Saturday, March 17, 2012 2:41 AM
  • Well, our codes were just templates of how to do it! It simply doesn't work just embedding them as fac-simile within your code.

    What you have to do is, when it comes the part inside your code it needs to await till the user clicks on his mouse; you reset the flag variable as "False", and only then, creates the conditional loop.

    ====================

    Clicked= "False

    While Clicked <> "True

      Program.Delay(100)

    EndWhile

    ====================

    So, the program awaits until the triggered Sub sets the variable Clicked as "True". It happens just after the user clicks a mouse button :P

    Then, the loop breaks and you know from that point on, the event happened!

    If you wish, just publish the part of your code you need help to debug. Cya!

    Saturday, March 17, 2012 7:03 AM
    Answerer
  • I see. Ill try that then
    • Proposed as answer by GAniman Saturday, March 17, 2012 6:17 PM
    Saturday, March 17, 2012 4:46 PM
  • gotoloop,

    i still couldnt get your solution to work but i thought of a much better one. just put what you want after the person has clicked into the sub.

    Example: Graphicswindow.drawboundtext (100, 100, 100, "Click to Continue")

    graphicswindow.mousedown = rat

    sub rat

    graphicswindow.clear

    graphicswindow.drawrectangle (100, 100, 100)

         endsub      


    • Edited by Stewart555 Saturday, March 17, 2012 5:23 PM
    • Proposed as answer by GAniman Saturday, March 17, 2012 6:17 PM
    Saturday, March 17, 2012 5:23 PM
  • Although your direct approach works as well, however, there is a glitch! Rat() will be executed every time a user clicks on his mouse, non-stop! That is, the event will happen anywhere in your code, not only when you want or need that to occur!
    Although you can turn it off by assigning Graphics.MouseDown= DoNothing sub, once you don't need it anymore.

    I know you're having some difficulties understanding an indirect approach to triggered events. But let's try it again! :D

    I've re-written the code to use 3 separate subs: Rat(), WaitMouseClick() and ClearScreen().

    Rat() is the triggered sub, WaitMouseClick() is responsible to just sitting-duck until user clicks one of the mouse buttons and ClearScreen() tidies up the screen.

    Here's the code; just study it and be not afraid to ask where you don't get it:

    '====================================================================
    GraphicsWindow.MouseDown= Rat    ' Activates MouseClick event
    
    GraphicsWindow.DrawText(10,10, "Please, click at the screen here to continue")
    
    WaitMouseClick()    ' Waits user's mouse click
    
    ClearScreen()       ' Clears GraphicsWindow
    
    ' Just some random code after mouse button event
    GraphicsWindow.DrawRectangle(100,100 200,200)  
    Sound.PlayClickAndWait()
    '====================================================================
    
    ' ---------------------------------------------------------------------------------------------------------------------
    Sub Rat
      ' Sets boolean flag to "True", to indicate a mouse button was clicked.
      
      Clicked= "True"   ' Boolean Flag is SET!!! ;-)
      
    EndSub
    ' ---------------------------------------------------------------------------------------------------------------------
    Sub WaitMouseClick
      ' Resets boolean flag variable to "False"  and awaits until it turns back into "True"
      'through Rat() triggered MouseDown event sub.
      
      Clicked= "False"
      
      While Clicked <> "True"
        Program.Delay(100)
      EndWhile
      
    EndSub
    ' ---------------------------------------------------------------------------------------------------------------------
    Sub ClearScreen
      ' Clears GraphicsWindow w/o erasing Shapes objects.
      
      GraphicsWindow.BrushColor= GraphicsWindow.BackgroundColor
      GraphicsWindow.FillRectangle(0,0, GraphicsWindow.Width, GraphicsWindow.Height)
      
    EndSub
    ' ---------------------------------------------------------------------------------------------------------------------

    • Edited by GoToLoopEditor Saturday, March 17, 2012 6:50 PM
    • Marked as answer by Stewart555 Sunday, March 18, 2012 3:47 PM
    Saturday, March 17, 2012 6:46 PM
    Answerer
  • ok i finally got it to work.

    Thank You

    Sunday, March 18, 2012 3:49 PM