Home
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Lego
Through
the Origin

Squares (2)

(previous pagetutorialsnext page)

Writing the Scripts

We have created one main stack Squares, with one card in it.  The card has one button and two fields.

That's a total of five objects.

Object-Oriented Programming

Here is a lot of theory, some of it will make sense only later on:

Everything in object-oriented programming works by

Events on the outside are mainly

Events are happenings with names such as MouseDown, MouseUp, OpenStack, EnterInField and so on.

Objects act by running event handlers.  An event handler is a piece of program code.

All event handlers of an object form that object's script.

In our little example, the thing we want to do is:

  1. type into the top field Number,
  2. then press the button Compute and
  3. see the square of the number appear in the lower field Square.

That is one way of designing the program and we will see other ways of doing it later.  For now, we'll just type into field Number, press the Compute button and look at field Square.

This means that field Number does not have to do anything, and neither does field Square.

But the button must do something:  it must take whatever is typed into field Number, square it and put the result into field Square.

Therefore, the only object that acts on an event is the button Compute.

There are many event messages a button can receive:  as you glide the mouse over a button, it will get the message MouseEnter, even without you clicking.  We don't want anything to happen then though.

It will also get the message MouseDown as soon as you push the mousebutton down (with the mouse inside the button).  But you may change your mind:  you may not want to do the computation and slide the mouse out of the button again before releasing it.  That would then generate a message MouseLeave.  We also don't want to do anything with that.  What we want is to do the computation when we release the mousebutton while it is on the button.  That event sends a MouseUp message.

Writing the MouseUp handler

Click once on the button Compute to select it, then click the triangle on the right hand side of the inspector, and select Edit Script:

The script window appears:

Note how it says in its title bar:  Script Editor - button "Compute" of card id 1002...

In that window, type the handler:

on MouseUp

put field "Number" into x

put x*x into field "Square"

end MouseUp

Note how Revolution immediately types the end MouseUp for you as soon as you hit the return key.  The line end MouseUp does not mean "stop the MouseUp".  It is just there to show how far the text of the handler goes.

The lines between the on MouseUp and end MouseUp are called program statements, or program code, or just statements.  Statements are executed by the program.

I used the intermediate variable x but you can use any name you like (except reserved words, but they will be shown in red or blue).  The variable x is also a container because it can hold a value.

It is not strictly necessary to put double quotes around the names of objects:  put field Number into x  would also have worked.  But there are cases where the double quotes must be used, and so it is a good habit always to use the double quotes around all names.  Like wearing seat belts:  always doing it protects from unforeseen circumstances.

Apply the script by clicking the Apply button at the bottom right of the script window:

Save your work.

(previous pagetutorialsnext page)





next planned revision:  2006-11-30