Livecode Wiki
Advertisement

Building the Code[]

A LiveCode program is a bunch of scripts[]

In LiveCode a program in not a single chunk of a thing in one place. It is numerous little pieces, each one is called a script. Most objects in a program contain scripts, which tell them what actions to take. Scripts contain one or more handlers or functions. If you are familiar with programming, you can think of handlers and functions as subroutines.

Handlers and functions response to events. As a result, LiveCode is called an event-driven language. One of the most common events is called mouseUp. To get an idea of this, imagine clicking a mouse button. First you would push down on it, that is a mouseDown event. Then you would release the mouse button and that is a mouseUp event. In other words, a mouseUp event is a click.

Add script to the Begin button[]

To add the following script to the Begin button, make sure you are in Edit mode. If not, then click on the upper right arrow button of the Tools Palette. Then, make sure you are on the first card of the stack. If not, then click on menu View > Go First. Finally, right click on the Begin Game button and click on Edit Script.

The Code Editor window should open. Notice that the title of this window at the top tells us exactly where the scrip is that we will be working on. Below that is a drop down menu called Handler List. This is a list of all the events that the button can respond to. Those same events are listed along the left side. Below the Handler List is a small colored button with the name of the object where the script is at. That small colored button gives the state of the script.

The first statement of a LiveCode handler always starts with the word “on,” followed by the name of the handler, followed by any parameters. The last statement of a handler always starts with the word “end,” followed by the name of the handler. Between the first statement and the last are all the statements that do something.

Now type in the following handler for the script of the Begin button:

global TotalGames
global TotalWon
on mouseUp
  global TotalGames
  global TotalWon
  put 0 into TotalGames
  put 0 into TotalWon
  put random(9) into field “FirstNumber” of card “Game” of this stack
  put random(9) into field “SecondNumber” of card “Game” of this stack
  put “?” into field “AnswerNumber” of card “Game” of this stack
  go to card “Game” of this stack
end mouseUp

Now we are going to add a mouseUp handler to the script of the Begin Game button on the Start card. This handler will execute when the user clicks on it within run mode.

The first two statements above are for global variables. They must be declared outside of any mouseUp handler. This mouseUp handler will execute when the user clicks on the Begin button in run mode. The second and third statements must be the global variables declared inside this handler where they are used. The third and fourth statements put zero into the global variables TotalGames and TotalWon.

The fifth statement puts a random number between 1 and 9 into field Number1 and a similar number into field Number2. The seventh statement will put a question mark into Answer1. The eighth statement moves the program to the second card so the game can begin. Then the handler ends.

Checking the Begin script[]

Changes have been made to this script, but the script has not been checked. As a result, the colored button will have turned yellow. To apply the check, click on the Apply button in the upper left hand corner of the Script Editor. If an error is found in the script, the colored button will turn red and you will have a problem to correct.

Check down at the bottom of the window, where the error messages are shown. Usually these messages are helpful, but sometimes not. Check out which statements are identified as faulty.

Now make sure your script matches the script at the top of this page. Also make sure that you personally typed in the script. If you cut and paste, sometimes LiveCode will mark the none-visual formatting as an error, everything will look OK, but it won’t execute. Check that the correct buttons and fields are placed on the right cards and stack, with the proper names. Check each of the statements in the Dictionary for the right format.

If errors were found and you corrected them, then click on the Apply button again. If no errors were found, then the colored button will turn green and the name of the handler will be added to the list on the left. This script is now ready to go.

With just this much of the program done, we can try it out to see it works. From the handler above, we should pick out those visual clues that it is working. In other words, we will be checking its runtime execution.

First of all, we should see no errors when the handler executes. If there are, you will have to correct them before proceeding. Second, we expect to see the program move to the second card. And third, there should be random numbers in the two fields and the third field a question mark.

Now make sure that LiveCode is in Run mode. If not, then click on the upper left arrow button on the Tools Palette. Then click on the Begin Game button. If this handler does what is expected, then you are ready to save it and go on to code the rest of the program. If not then you must debug the handler until it works.

Add script to the Score button[]

Now switch the program onto the second card. Then open up the Code Editor for the Find Score button by right clicking on the button and clicking on Edit Code. Now type in the following handler for the script of the Find Score button:

on mouseUp
  global TotalGames
  global TotalWon
  if field “AnswerNumber” = empty then exit mouseUp
  add 1 to TotalGames
  put field “FirstNumber” + field “SecondNumber” into TempAnswer
  if field “AnswerNumber” = TempAnswer then add 1 to TotalWon
  answer “Score =” && 100*TotalWon/TotalGames
  put random(9) into field “FirstNumber”
  put random(9) into field “SecondNumber”
  put empty into field “AnswerNumber”
end mouseUp


The above mouseUp handler will execute when the user clicks on the Find Score button in run mode.

Advertisement