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:
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:
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.
e.g.
notes:
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:
Try the following:
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
(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
Assignment: NEXT - Making Decisions when you have finished... |
Home >








