none
Buttons don't seem to be working? RRS feed

  • Question

  • I have been trying to create a small game where you press a button to go forward, back, left right, etc. My goal is to have a picture displayed, and then when you press forward the picture switches to a new one. Except when I press the button, nothing seems to happen. Im just starting out at Small basic, and I guess I just must be missing something. Any help? Here is my code:

    b1 = Controls.AddButton("Forward", 10, 10,)
    b2 = Controls.AddButton("Back", 10, 50)
    pic = "C:\Users\John\Documents\My Games\other\Tunnel3.jpg"
    pic2 = "C:\Users\John\Documents\My Games\other\3707988218_6b9c192459.jpg"
    Sub OnButtonClicked
      
      Button= Controls.LastClickedButton
      
      If Button = b1 Then
        GraphicsWindow.DrawResizedImage(pic, 20, 20, 20 20)
      EndIf
      If Button = b2 Then
        GraphicsWindow.DrawResizedImage(pic2, 40, 40, 40, 40)
      EndIf
      
      EndSub

    Sunday, April 15, 2012 2:08 AM

Answers

  • Welcome aboard Demosthenes466!

    You just forgot to activate the button event to use your Sub OnButtonClicked. It means OnButtonClicked() is never invoked!
    And CodingLikeCrazy, Controls.GetButtonCaption() got nothing to do with that. It's just an alternative way to access buttons using their captions, and thus, no need to assign variables to them when they are created.

    So Demos, I've slightly modified your code so you can see how it works. Here it comes:

    Path = "http://ximages.net/images/"       '  Since I don't have your local pics, using ones from this site   ;-)
    pic1 = Path + "28930020147868715423.png"
    pic2 = Path + "03515726579119112960.png"
    
    Controls.AddButton("Forward"  10,10)
    Controls.AddButton("Back"     10,50)
    
    Controls.ButtonClicked = OnButtonClicked   ' <----  You forgot this! It turns on the button click event
    
    Sub OnButtonClicked
      
      Button= Controls.GetButtonCaption( Controls.LastClickedButton )  '  Alternative way using button's caption
      
      If Button = "Forward" Then
        GraphicsWindow.DrawResizedImage(pic1  180,20  60,40)
      EndIf
      
      If Button = "Back"    Then
        GraphicsWindow.DrawResizedImage(pic2  240,40  40,40)
      EndIf
      
    EndSub
    Sunday, April 15, 2012 5:35 AM
    Answerer
  • Using GoToLoop's code just made some changes, you must correct the path of the pictures for it to work properly

    used arrays for the pictures to be able to make a slideshow like program much more easily using a counter for the index

    path = "C:\Users\Miguel\Pictures\"   '  Same as with GoToLoop i dont have your pictures so i'm using random ones from my computer
    'I will be using arrays so the variables for the pictures are now declared here, as many as you want
      pic[1] = ImageList.LoadImage(path + "{2043A3A4-081D-447C-8A2D-7BB268F686BC}_monterio_g.gif")
      pic[2] = ImageList.LoadImage(path + "31aa1e6f6319751b8f8e8dcfb7be78d3.jpg")
      pic[3] = ImageList.LoadImage(path + "Base Black.png")
      pic[4] = ImageList.LoadImage(path + "splash_admat(rev3-1)_01.jpg")
      pic[5] = ImageList.LoadImage(path + "evolution_1.jpg")
    
    Controls.AddButton("Forward"  10,10)
    Controls.AddButton("Back"     10,50)
    
    i = 0 'A regular counter variable
    
    Controls.ButtonClicked = OnButtonClicked   ' <----  You forgot this! It turns on the button click event
    
    Sub OnButtonClicked
      
      Button= Controls.GetButtonCaption( Controls.LastClickedButton )  '  Alternative way using button's caption
      
      If Button = "Forward" And i < 5 Then 'And i < 5 added for not been able to draw pic[6] that  doest exist
        i = i + 1
        GraphicsWindow.DrawResizedImage(pic[i]  180,100  100,100) ' switched sizes for biger pictures
      EndIf
      
      If Button = "Back"  And i > 1  Then ' same as in line 20
        i = i - 1
        GraphicsWindow.DrawResizedImage(pic[i]  180,100  100,100)
      EndIf
      
    EndSub


    • Edited by MikeNeto Sunday, April 15, 2012 1:56 PM
    • Marked as answer by Demosthenes466 Sunday, April 15, 2012 5:41 PM
    Sunday, April 15, 2012 1:55 PM

All replies

  • When I use subs someone on the forums name go to loop thought me to use GetButtonCaption of the LatButtonClicked see thread:

    http://social.msdn.microsoft.com/Forums/en-US/smallbasic/thread/bc0eb3bb-375b-4ddf-a3ef-96c494f7c38a

    -------------------------------------------------------------------------------------

    Remember to make best answer!

    Sunday, April 15, 2012 3:10 AM
  • Welcome aboard Demosthenes466!

    You just forgot to activate the button event to use your Sub OnButtonClicked. It means OnButtonClicked() is never invoked!
    And CodingLikeCrazy, Controls.GetButtonCaption() got nothing to do with that. It's just an alternative way to access buttons using their captions, and thus, no need to assign variables to them when they are created.

    So Demos, I've slightly modified your code so you can see how it works. Here it comes:

    Path = "http://ximages.net/images/"       '  Since I don't have your local pics, using ones from this site   ;-)
    pic1 = Path + "28930020147868715423.png"
    pic2 = Path + "03515726579119112960.png"
    
    Controls.AddButton("Forward"  10,10)
    Controls.AddButton("Back"     10,50)
    
    Controls.ButtonClicked = OnButtonClicked   ' <----  You forgot this! It turns on the button click event
    
    Sub OnButtonClicked
      
      Button= Controls.GetButtonCaption( Controls.LastClickedButton )  '  Alternative way using button's caption
      
      If Button = "Forward" Then
        GraphicsWindow.DrawResizedImage(pic1  180,20  60,40)
      EndIf
      
      If Button = "Back"    Then
        GraphicsWindow.DrawResizedImage(pic2  240,40  40,40)
      EndIf
      
    EndSub
    Sunday, April 15, 2012 5:35 AM
    Answerer
  • Using GoToLoop's code just made some changes, you must correct the path of the pictures for it to work properly

    used arrays for the pictures to be able to make a slideshow like program much more easily using a counter for the index

    path = "C:\Users\Miguel\Pictures\"   '  Same as with GoToLoop i dont have your pictures so i'm using random ones from my computer
    'I will be using arrays so the variables for the pictures are now declared here, as many as you want
      pic[1] = ImageList.LoadImage(path + "{2043A3A4-081D-447C-8A2D-7BB268F686BC}_monterio_g.gif")
      pic[2] = ImageList.LoadImage(path + "31aa1e6f6319751b8f8e8dcfb7be78d3.jpg")
      pic[3] = ImageList.LoadImage(path + "Base Black.png")
      pic[4] = ImageList.LoadImage(path + "splash_admat(rev3-1)_01.jpg")
      pic[5] = ImageList.LoadImage(path + "evolution_1.jpg")
    
    Controls.AddButton("Forward"  10,10)
    Controls.AddButton("Back"     10,50)
    
    i = 0 'A regular counter variable
    
    Controls.ButtonClicked = OnButtonClicked   ' <----  You forgot this! It turns on the button click event
    
    Sub OnButtonClicked
      
      Button= Controls.GetButtonCaption( Controls.LastClickedButton )  '  Alternative way using button's caption
      
      If Button = "Forward" And i < 5 Then 'And i < 5 added for not been able to draw pic[6] that  doest exist
        i = i + 1
        GraphicsWindow.DrawResizedImage(pic[i]  180,100  100,100) ' switched sizes for biger pictures
      EndIf
      
      If Button = "Back"  And i > 1  Then ' same as in line 20
        i = i - 1
        GraphicsWindow.DrawResizedImage(pic[i]  180,100  100,100)
      EndIf
      
    EndSub


    • Edited by MikeNeto Sunday, April 15, 2012 1:56 PM
    • Marked as answer by Demosthenes466 Sunday, April 15, 2012 5:41 PM
    Sunday, April 15, 2012 1:55 PM
  • Thank You! I Hadnt realized that even though I had set the event up, I had to activate it using Controls.ButtonClicked = OnButtonClicked. Its Working great now!
    Sunday, April 15, 2012 5:04 PM
  • I started out with GoToLoop's code and that worked great for the first few pictures, but then I struggled to add new pictures in. I came back and saw your post, and saw that it allowed me to more easily add more pictures so that pressing forward multiple times will cycle through more photos. Thank you!
    Sunday, April 15, 2012 5:42 PM
  • Hello again Demonsthenes!

    I confess I had no idea what you were trying to accomplish! Like I said, just slightly tweaked your code, haven't added up any extra fancy trick there.

    Just wanted to publish a Flickr alternative version. Of course, resuming where MikeNeto stopped.  ;-P

    Import code is MTG686. Although it's not working w/ Silverlight plugin for me!

    gw = 1250
    gh = 750
    imgmax = 5
    tag = "Tunnel"
    
    GraphicsWindow.Width  = gw
    GraphicsWindow.Height = gh
    GraphicsWindow.Left = (Desktop.Width  - gw) / 2  '  Centralizes GraphicsWindow.
    GraphicsWindow.Top  = (Desktop.Height - gh) / 2
    GraphicsWindow.BackgroundColor = "Black"
    
    LoadImages()   '  Load imgmax number of pictures.
    
    Controls.AddButton("Forward"  10,10)
    Controls.AddButton("Back"     10,50)
    Controls.AddButton("Quit"     10,110)
    
    img = 1            '  Starting from 1st picture. img is index for pic[] array.
    OnButtonClicked()  '  Directly invoking it to display a picture aSaP.
    
    Controls.ButtonClicked = OnButtonClicked
    
    ' --------------------------------------------------------------------------------------------------------------------------------------------------'
    Sub OnButtonClicked
      
      Button= Controls.GetButtonCaption( Controls.LastClickedButton )
      Sound.PlayClick()
      
      If Button = "Forward"  Then
        img = img + 1
      ElseIf Button = "Back" Then
        img = img - 1
      ElseIf Button = "Quit" Then
        Sound.PlayChimeAndWait()
        Program.End()
      EndIf
      
      If img > PicCount Then
        img = PicCount
      ElseIf img < 1    Then
        img = 1
      EndIf
      
      GraphicsWindow.DrawResizedImage(pic[img]  100,30  gw-140,gh-60)
      GraphicsWindow.Title = "Image #" + img
      
    EndSub
    ' --------------------------------------------------------------------------------------------------------------------------------------------------'
    Sub LoadImages
      
      For img = 1 To imgmax
        GraphicsWindow.Title = "Loading #" + img
        Sound.PlayChimes()
        pic[img] = ImageList.LoadImage( Flickr.GetRandomPicture(tag) )
      EndFor
      
      PicCount = Array.GetItemCount(pic)   '  Redundant to img_max. Using just for example purposes.
      
    EndSub
    ' --------------------------------------------------------------------------------------------------------------------------------------------------'
    Sunday, April 15, 2012 11:36 PM
    Answerer
  • awesome program GoToLoop the code is clean and easy to understand and i really loved the sounds and the loading screen idea, Great Job ^^
    Sunday, April 15, 2012 11:56 PM
  • Thanks. I'm glad you liked it!   (^_^)
    Monday, April 16, 2012 12:12 AM
    Answerer
  • Extended the code once more. Meet the newest Flickr Buttons - Hyper Modularized Version!

    Import code -> MTG686-3

    Monday, April 16, 2012 2:53 AM
    Answerer