Home‎ > ‎

First Programs - Interacting with the User (Answer, Ask commands)

We are going to get started and you are going to learn how to pop-up dialog boxes and get information from the user. If you have not already done so, look at the Lesson1_Intro from the LiveCode people at RunRev. We are going to build on that and you will be doing consecutively harder programs as a way of learning LiveCode.

Dialog Boxes - Informational Boxes - "Answer" commands
The first LiveCode command we will learn is the "Answer" command. It enables you to pop-up a dialog box giving information to the user. 

1. The answer command - a dialog box with buttons
    It's basic form is:
         answer "message"      

    e.g. answer "Hello World"

    Your First Program
    Let's do a simple program that pop-ups a box saying "welcome" when the user presses a button:
  • Create a new Mainstack (from the previous lesson) and name and save it as "Hello"
  • Drag over any button from the Tools Palette to the blank card
  • right-click on it and select "Property Inspector"
  • In the Property Inspector, change the name from "button" to "Hello", click elsewhere to set it and then close the Property Inspector
  • Right-click on the button again (note that it is now called "Hello") and select "Edit Script"
  • Add the line - Answer "Hello"
  • It should now look like this:
on mouseUp
   answer "Hello"
end mouseUp

    You should see the following on your screen after doing the above...

                      

    Now when you run the program and press the mouse button down, then let go of it you should see a box pop-up with the word "Hello" in it

    Let's see if it works:

  • Close the Code Editor
  • Click on the "Run" tool on the Tools Palette, then click on your "Hello" button.

    Congratulations, you have just written your very first computer program


                   


Screenshots of the Above Steps

    click here - First Program - to see the screenshots showing the above steps:

Moving Ahead...
    (You should try each code segment yourself, for practice. Do not just read this and do the last one. Do each step, its fun.)


Using the answer command with buttons

    The Answer command has an option where you can put up a Dialog message box with up to seven (7) buttons. 

    The syntax is:

        Answer <prompt> with <button1> or <button2> ...titled <title>

    where the button that the user clicked on is returned in "it"

        e.g.

    on mouseUp
        answer "Do you want to continue?" with "Yes" or "No" titled "Continue?"
        if it is "No" then 
  answer "Okay, Bye"
             exit mouseUp
        end if
        answer "I am glad that you said yes"
   end mouseUp

  
                 


notes:
  • The last button listed in the command will be the default button
  • The button name will be returned in the variable "it"
  • You can exit the handler with the "exit" command
  • This is useful to control or restrict the answers that the user can return
This is a little confusing because we used the Ask command to get info in the last lesson. Think of it this way:
    The Answer command only has buttons for the user to click on. The basic one has only the "Okay" button but with the "with", you can add your own buttons for the user to click-on
    The Ask command has a field for the user to type in any information



2. The ask command - a dialog box with a text entry field
    We can move ahead and learn some more commands and modify our program to do even more. Let's ask the user his/her name. We use the "ask"     command to do this.

    Its basic form is:
        ask "question"  
    
    e.g. ask "How are you?"

    where it puts up a Dialog box asking the question, and has a field where the user can type their answer which is returned to you in "it"

    Interacting with the User
        We will now ask the user's name using ask.

        Change the code on the "Hello" button to:

on mouseUp
   ask "What is your name?"
   answer "Hello"
end mouseUp

       Run it to see how it works


                     

Wouldn't it be nice to welcome them by their name? We asked them their name but when the Dialog Box asking them their name went away, so did what they typed. We have to save their name in a variable. That way, we will have it for later. (A variable is like a "box" where we can save what they type) In this case, we will call the variable "theName"

    Now try this code:

on mouseUp
   ask "What is your name?"
   put it into theName
   answer "Hello " & theName
end mouseUp

   What happened?  


                     

    If you look at every line, you can see what took place and make sense of it.


Questions to answer:

Variations of the  Ask Command
Look up the command in the Dictionary (click on the "Dictionary" icon in the top "Edit Bar" to see what else that you can do. (select the "answer" command)

The differences between the  ask and the  answer Command
   Try the following:
    answer "How old are you?" with "13" or "14"  or "15"  or "16"
    ask "How old are you?" 

   1. What is the advantage of each way? 
   2. Which one is simpler? Which one can you be sure of the answer? 
   3. Which one handles every possible age? Which one allows "thirteen" or "thirdteen"?

Testing the command on your own:
Click on the "Message Box" icon on the top menubar of Livecode. This opens up a window where you can test out commands on your own. Just type the command in the window, press the "Return" or "Enter" key and see what the command does

For instance try the following: (see if you can tell what is new or different)
    ask "What is your Name?"
    ask question "What is your Name?"
    ask question "What is your Name?" titled "Give us your name"
    ask question "What is your Name?" titled "Give us your name" with "Willy Wonka"
    ask question "What is your Name?" with "your name goes here"

    4. What does each part of the command do?


Challenge:
    Now it is your turn. Add more code to ask the user what class he/she is in....


note: There is nothing special about the variable - theName , we could just have used "x"

on mouseUp
   ask "What is your name?"
   put it into x
   answer "Hello " & x
end mouseUp


on mouseUp
   ask "What is your name?"
   put it into name
   answer "Hello " & name
end mouseUp

(Sometimes it is hard to tell which are special (reserved) words in the language and which are made-up ones. For instance, "Name" is a property of every object. So we can not use it - or if we do, LiveCode could get confused. for that reason, I use "x" and other single letters as my examples. As you get better, you can use variable names that describe the variable - like theName, yourName, aName, theUsersName, etc. Your programs will be easier to read and you do not try to remember what 'x" was for)

ADVANCED - IMPORTANT - ACCEPTED STANDARDS and CONVENTIONS in LIVECODE
        In naming variables, most people put a letter in front of the name so they can tell where it was declared.
            s - for screen variable
            g - for global variables
             t - for temporary variables (local variables)
            etc

        So you would have the following variables in your program
            gName
            tCounter
            sScoreWindow

    You will see this convention followed in the scripts that you get on the LiveCode site, or in the forums from other people. It would be wise for you to follow them too !!

ADVANCED WORK: The Message Box

Variations of the  Answer Command
Look up the command in the Dictionary (click on the "Dictionary" icon in the top "Edit Bar" to see what else that you can do. (select the "answer" command)

Testing the command on your own:
Click on the "Message Box" icon on the top menubar of Livecode. This opens up a window where you can test out commands on your own. Just type the command in the window, press the "Return" or "Enter" key and see what the command does






For instance try the following: (see if you can tell what is new or different)
    answer information "You clicked me"
    answer information "You clicked me" titled "Welcome"
    answer question "Why did you click me again?"
    answer "Do you want to continue?" with "Yes" or "No"
    answer "Do you want to continue?" with "No" or "Yes"


Assignment:
           Greeting Program


NEXT -  Making Decisions when you have finished...


Ċ
cyril.pruszko@pgcps.org,
Dec 17, 2012, 7:17 PM
Ċ
cyril.pruszko@pgcps.org,
Dec 9, 2012, 3:16 AM
Comments