none
How do the buttons work? RRS feed

  • Question

  • Ok, now before I will ask the thing i want to understand, my goal is this:

    making a little funny program does the following:

    A button comes out (with a text in it)

    When you click it, it will play a sound, then followed by other. While the sound is being played the button can't be used anymore until the sound is over.

    When it's over, the text of the button changes (because it's like this is your reply to the question - that will be played in sounds)

    So every time i click the button, play a series of sounds tracks, make the user wait until it's over, then give him the possibility to click again continuing the game. (I will just make a guy get insulted all the times when he clicks it xD)

     

    Well for now i started small. Now I don't know how to make the text in the button change.

     

    This is what i made until now:

    btnStart = Controls.AddButton("Start", 100, 50)

    Controls.ButtonClicked = OnClick

    Sub OnClick
      If Controls.LastClickedButton = btnStart Then
        Sound.Stop("C:\Users\Sandu\Desktop\Crap\gentelmen.wav")
        Sound.Play("C:\Users\Sandu\Desktop\Crap\gentelmen.wav")
      EndIf
    EndSub

     

    I suppose i need to use something like a cicle (i=i+1) and arrays. o.O 

    So? any ideas? How to make the button change it's text after the first click, and wait until the sound is over, now giving the change to click again.

    Ty

    • Changed type litdevModerator Saturday, January 29, 2011 6:35 PM This is a question
    Saturday, January 29, 2011 4:58 PM

Answers

  • Here is a bit of code to try:

    Note the following:

    1] The Sound.PlayAndWait for the sound does just that

    2] The button can be shown and hidden with Controls.ShowControl a nd Controls.HideControl.  We could disable the button instead of hiding it, by not accepting the button press while the sound is being played, but a bit more work to do it this way.

    3] The button caption is updated with Controls.SetButtonCaption

    4] I use a main loop that runs all the time using a flag (clicked ) set by the button press - the reason is that this method reliably handles things like the PlayAndWait that have problems working inside of the event subroutines because the event subroutines are running on new threads at the same time (sort of parallel) as the main code and can result in problems for some commands like the PlayAndWait that themselves get started on new threads.

    tryCount = 1
    btnStart = Controls.AddButton("Try "+tryCount, 100, 50)

    Controls.ButtonClicked = OnClick

    'A continual loop that just keeps running and processing the program depending on flags set by events
    While ("True")
      If (clicked = 1) Then
        clicked = 0 'This if will not be entered again until a new click is made - not possible until the sound has finished
        'Hide button and do the sound
        Controls.HideControl(btnStart)
        Sound.Stop(Program.Directory+"\test.wma")
        Sound.PlayAndWait(Program.Directory+"\test.wma") 'Wait for sound to finish (button is hidden duting this)
        'Reshow and update button
        tryCount = tryCount+1
        Controls.SetButtonCaption(btnStart,"Try "+tryCount)
        Controls.ShowControl(btnStart)
      EndIf
      Program.Delay(20) 'Prvent mashing cpu while waiting
    EndWhile

    Sub OnClick
      If (Controls.LastClickedButton = btnStart) Then
        clicked = 1
      EndIf
    EndSub

    • Marked as answer by Sandu92 Monday, January 31, 2011 5:00 PM
    Saturday, January 29, 2011 6:24 PM
    Moderator

All replies

  • Here is a bit of code to try:

    Note the following:

    1] The Sound.PlayAndWait for the sound does just that

    2] The button can be shown and hidden with Controls.ShowControl a nd Controls.HideControl.  We could disable the button instead of hiding it, by not accepting the button press while the sound is being played, but a bit more work to do it this way.

    3] The button caption is updated with Controls.SetButtonCaption

    4] I use a main loop that runs all the time using a flag (clicked ) set by the button press - the reason is that this method reliably handles things like the PlayAndWait that have problems working inside of the event subroutines because the event subroutines are running on new threads at the same time (sort of parallel) as the main code and can result in problems for some commands like the PlayAndWait that themselves get started on new threads.

    tryCount = 1
    btnStart = Controls.AddButton("Try "+tryCount, 100, 50)

    Controls.ButtonClicked = OnClick

    'A continual loop that just keeps running and processing the program depending on flags set by events
    While ("True")
      If (clicked = 1) Then
        clicked = 0 'This if will not be entered again until a new click is made - not possible until the sound has finished
        'Hide button and do the sound
        Controls.HideControl(btnStart)
        Sound.Stop(Program.Directory+"\test.wma")
        Sound.PlayAndWait(Program.Directory+"\test.wma") 'Wait for sound to finish (button is hidden duting this)
        'Reshow and update button
        tryCount = tryCount+1
        Controls.SetButtonCaption(btnStart,"Try "+tryCount)
        Controls.ShowControl(btnStart)
      EndIf
      Program.Delay(20) 'Prvent mashing cpu while waiting
    EndWhile

    Sub OnClick
      If (Controls.LastClickedButton = btnStart) Then
        clicked = 1
      EndIf
    EndSub

    • Marked as answer by Sandu92 Monday, January 31, 2011 5:00 PM
    Saturday, January 29, 2011 6:24 PM
    Moderator
  • Oh, now there is a thing on which i was having a doubt. That tryCount. So that variables value can be increased even while it is in a sub? now i am getting an idea of how to make up the Question count. xD thx for helping. I'll reply also some other post if i get more problems (also showing my progress).

     

    o.o is While ("True") a special command?

    Saturday, January 29, 2011 7:07 PM
  • Yes, in SmallBasic (unlike most programming languages) all variables have Gloabal Scope or called Static - this means they can be used or changed in any part of the code - usually this makes things easier for the smaller program people while in SmallBasic, but you should be careful - check out what this doesn't work as you might expect.

    'Creates and moves only one ball
    For i = 1 To 10
      ball[i] = Shapes.AddEllipse(20,20)
      Update()
    EndFor

    Sub Update
      For i = 1 To 10 'Error using i here, when it is also used in the calling loop!
        Shapes.Move(ball[i],50*i-10,50*i-10)
      EndFor
    EndSub

    Saturday, January 29, 2011 7:17 PM
    Moderator
  • Man I just love that example of yours. Using that while to activate a sound when the Clicked variable has 1, then make it immediately 0 to not repeat the instructions, and that little sub to manipulate Clicked.

    Now i inserted some array, and defined at the start paths (for the audio play) and sentences that will be written as name of the button. I used your tryCount as my counter. Lol this is just great.

    col[1] = "C:\Users\Sandu\Desktop\Jacket\come ti chiami.mp3"
    say[1] = "Come ti chiami faccia di merda?"
    col[2] = "C:\Users\Sandu\Desktop\Jacket\io scometto.mp3"
    say[2] = "Io scometto che tu sei uno di ..."

    tryCount = 1
    btnStart = Controls.AddButton("Parla con me!",100,50)

    Controls.ButtonClicked = OnClick

    While "True"
      If clicked = 1 Then
        clicked = 0
        Controls.HideControl(btnStart)
        Sound.Stop(col[tryCount])
        Sound.PlayAndWait(col[tryCount])
        tryCount = tryCount + 1
        Controls.SetButtonCaption(btnStart, say[tryCount])
        Controls.ShowControl(btnStart)
      EndIf
      Program.Delay(20)
    EndWhile

    Sub Onclick
      If Controls.LastClickedButton = btnStart Then
        clicked = 1
      EndIf
    EndSub

    Ok now. For the next level i will try to work out how to also insert certain photos when the sound is played. xD This is so funny.

    Thx a lot for the tips.

     

    ps - that is Italian language.

    Saturday, January 29, 2011 7:36 PM
  • This is it a bit extended to make the button inactive rather than hiding it by more complex use of the clicked flag with 3 states: 0-waiting for click 1-just clicked 2-doing the clicked work and button inactive.

    tryCount = 1
    btnStart = Controls.AddButton("Try "+tryCount, 100, 50)

    clicked = 0 'Initialise the clicked to be the starting ready condition
    Controls.ButtonClicked = OnClick

    'A continual loop that just keeps running and processing the program depending on flags set by events
    While ("True")
      If (clicked = 1) Then
        clicked = 2 'Set the clicked flag so it won't do this IF or the EVENT
        'Do the sound
        Sound.Stop(Program.Directory+"\test.wma")
        Sound.PlayAndWait(Program.Directory+"\test.wma") 'Wait for sound to finish (button is inactive during this)
        'Update button
        tryCount = tryCount+1
        Controls.SetButtonCaption(btnStart,"Try "+tryCount)
        clicked = 0 'Ready for next click
      EndIf
      Program.Delay(20) 'Prvent mashing cpu while waiting
    EndWhile

    Sub OnClick
      If (clicked = 0 And Controls.LastClickedButton = btnStart) Then 'Only do the event if we are ready for it
        clicked = 1
      EndIf
    EndSub

    Saturday, January 29, 2011 7:57 PM
    Moderator