|

The rest of the script is just an if statement that tests whether the value of nClicks is greater than 4, and displays a message when that's true. (A couple more concatenation operators are used in the Dialog.Message action, to create the string for its second parameter. Note that you can easily include the contents of a variable in the "middle" of a string as well.)


Once you're done editing the button's On Click script, click OK to close the script editor.


3 ) A dd t h e f ollowing lin e t o t h e p a g e ' s On P re lo a d eve n t: n C li ck s = 0 ;


We need to initialize nClicks before the button is clicked, so that our script in the button's On Click event will start counting up from 0. Note that we can't just set nClicks to 0 in the button's On Click event, or it would be reset to zero every time the button was clicked.


To add the line to the page's On Preload event, just double-click on the page surface, then click on the On Preload tab of the Script editor, and add the nClicks line to the existing script. Once you're done, it should like something like this:


image


It doesn't matter if you put the nClicks = 0; line before or after the existing line...the order of the individual lines in this script isn't important.


4 ) P rev i e w t h e p r oj ec t , a nd c li ck on t h e bu tt on obj ec t 5 t i me s .

Each time you click on the button, its text will change to show how many times you've clicked on it so far. After you've clicked on the button 5 times, it will also display a message telling you how many times you've clicked.


5 ) E x i t t h e p rev i e w.

image

There you go. You've just created a pretty sophisticated little program.


Tip: If you feel adventurous, see if you can modify the script to stop announcing the number of clicks once you've clicked 10 times.


Here's a hint: you'll need to use a logical operator like "or" and "and" to test more than one thing at once in the if statement's condition. For example, you could make the test only be true between 3 and 7 by using a condition like this:


if (nCount > 2) and (nCount < 8) then

-- only true if greater than 2 and less than 8

end


The second line in the above script is a comment. (It doesn't actually do anything.) Everything on a line after two dashes (--) is completely ignored by AutoPlay. You can use such comments wherever you'd like to add "internal" notes to your scripts.


Using a For Loop

Sometimes it's helpful to be able to repeat a bunch of actions several times, without having to type them over and over and over again. One way to accomplish this is by using a for loop.


The basic syntax of the for loop is:


for variable = start, end, step do

do something here

end


For example:


for n = 10, 100, 10 do Dialog.Message("", n);

end


The above script simply counts from 10 to 100, going up by 10 at a time, displaying the current value of the variable n in a dialog message box. This accomplishes the same thing as typing:


Dialog.Message("", 10);

Dialog.Message("", 20);

Dialog.Message("", 30);

Dialog.Message("", 40);

Dialog.Message("", 50);

Dialog.Message("", 60);

Dialog.Message("", 70);

Dialog.Message("", 80);

Dialog.Message("", 90);

Dialog.Message("", 100);


Obviously, the for loop makes repeating similar actions much easier.


Let's use a simple for loop to add all of the digits between 1 and 100, and display the result.


1 ) A dd a bu tt on obj ec t t o t h e p a g e . A dd t h e f ollowing s cr ip t t o t hi s bu tt on obj ec t ' s On C li ck eve n t:


n = 0 ;

f o r i = 1 , 100 do n = n + i ;

e nd

D i a log. M e ss a g e ("" , " Th e s u m o f a ll t h e digi t s i s : " .. n ) ;


The script should look like this when you're done:


image


The first line creates a variable called n and sets it to 0. The next line tells the for loop to count from 1 to 100, storing the current "count" at each step in a variable named i.


During each "pass" through the loop, the script between the "do" and the "end" will be performed. In this case, it consists of a single line that adds the current values of n and i together, and then stores the result back into n. In other words, it adds i to the current value of n.


This for loop is the same as typing out:


n = n + 1; n = n + 2; n = n + 3; n = n + 4;


...all the way up to 100.


The last line of our script just displays the result of the calculation to the user in a dialog message box.


image

Tip: You can use the Add Code button to insert an example for loop, complete with comments explaining the syntax. You can then edit the example to fit your own needs.


2 ) P rev i e w t h e p r oj ec t , a nd c li ck on t h e bu tt on obj ec t .

When you click on the button object, the for loop will blaze through the calculation 100 times, and then the Dialog.Message action will display the result.


It all happens very fast.


image


3 ) E x i t t h e p rev i e w.

You probably won't find yourself using for loops as often as you'll use if statements, but it's definitely worth knowing how to use them. When you need to repeat steps, they can save you a lot of effort.


Of course, when it comes to saving you effort, the real champions are functions.


Creating Functions

A function is just a portion of script that you can define, name and then call from somewhere else.


You can use functions to avoid repeating the same script in different places, essentially creating your own custom "actions"-complete with parameters and return values if you want.


In general, functions are defined like this:


function function_name (arguments)

function script here

return return_value;

|