app-inventor

Set click button events in a loop


In my application MIT App Inventor, i created a function to click on the image:

to myfunc ( button )
  do ...

there are several buttons with the same actions:

when image1.Touched
  x  y
do call myfunc
          button( image1 )

when image2.Touched
  x  y
do call myfunc
          button( image2 )

when image3.Touched
  x  y
do call myfunc
          button( image3 )
... 

I have 20 images. Can I set a click events for them inside a loop?


Solution

  • you can call the same procedure in each button click event and pass the button id as input parameter

    when button1.Click do 
      call myProcedure "1"...
    
    when button2.Click do 
      call myProcedure "2"...
    
    when button3.Click do 
      call myProcedure "3"...
    
    when button4.Click do 
      call myProcedure "4"...
    

    then in a for each loop you could do something like this (assuming there are 4 buttons) and pass the current number to the procedure

    for each number from 1 to 4 do
      call procedure "number"
    

    A very good way to learn App Inventor is to read the free Inventor's Manual here in the AI2 free online eBook http://www.appinventor.org/book2 ... the links are at the bottom of the Web page. The book 'teaches' users how to program with AI2 blocks. There is a free programming course here http://www.appinventor.org/content/CourseInABox/Intro and the aia files for the projects in the book are here: http://www.appinventor.org/bookFiles
    How to do a lot of basic things with App Inventor are described here: http://www.appinventor.org/content/howDoYou/eventHandling .

    Also do the tutorials http://appinventor.mit.edu/explore/ai2/tutorials.html to learn the basics of App Inventor, then try something and follow the Top 5 Tips: How to learn App Inventor