Runtime Revolution
 
Articles Other News

Cannonballs! Part 1.2


by Robert Cailliau


Putting in some controls

Gunmen turn the gun barrel up or down in order to reach the target. The angle at which the barrel is turned is called the elevation.

We need some way to turn the barrel. One is to use a control called a scrollbar. Buttons, fields, sliders, scrollbars etc. are called controls.

Don't worry until you get to the explanations further down.

Drag a slider out of the tools palette:

Drag a scale in the grass:

Using the inspector, give the name Elevation to this scrollbar:

Select the label field tool:

and drag a label field in the grass:

Finally, select the normal field tool and drag a field in the grass:

Using the inspector, name the field we just made Elevation and set its properties as follows:

<

That makes the field transparent (not opaque), so the grass colour will show through; it locks its text so you cannot modify it, and it removes its border.

Select the label field and using the inspector name it hElevation and set its properties the same as for the Elevation field. Then select the contents mode in the inspector and put the word Elevation into the contents box:

Finally, position the three objects we created in this arrangement:

You will have nothing shown in the Elevation field, whereas the above figure taken from my screen shows the value 00. Don't worry.

What was all that?

We need the slider scale to adjust the elevation of the barrel, but we want to have a label to show what this slider is for. That is the function of the label field hElevation. It has for contents the text "Elevation". We want the angle of elevation to show as a number, that is the reason for the field Elevation.

I used the letter h in front of hElevation to mean that the field is just a heading. A label field is in fact a normal field, it just has some properties already set to function as a label. It is part of the decoration and not used in any scripts.

Note that we have two objects with the name Elevation: a scrollbar and a field. That's no problem, because inside scripts we need to address them as scrollbar "Elevation" and field "Elevation" , so there can be no confusion. However, the label field for these two must have a different name since there should not be two fields with the same name.

Good, but these objects don't do anything yet. We need some scripts.

Setting the Elevation of the Barrel

When we move the slider on the scale, we want the barrel to turn up. The lowest value is 0 degrees, the highest value is straight up, 90 degrees.

As soon as you start dragging the blue slider button on the scale scrollbars, the scale object gets a stream of messages ScrollbarDrag. These we must use to turn the barrel. Open the script window of the scale scrollbar, using the inspector, and type this handler:

on ScrollbarDrag fValue

set the angle of image "CannonBarrel.png" to fValue

put fValue into field "Elevation"

end ScrollbarDrag

Two important things about this handler:

  1. There is a parameter called fValue. This is the name I used for the value that Revolution sends to the handler to indicate where the blue button is on the scale. I use the prefix letter f (for formal parameter) to name parameters.
  2. We set the property angle of the image to that value, so the barrel image will turn. We also put that value into the Elevation field, so we humans can see the number.

You can now Apply the script, save your work, pick the browser tool and move the blue button on the scale to see what happens.

Surprise!

Surprise indeed: the barrel turns far too quickly and seems to turn the wrong way around too! That is because we did not tell the srcollbar Elevation what the limits were. Using the inspector, set the end value of the slider to 90:

Now the value will be 0 when the button is at the extreme left of the scale, and 90 when it is at the extreme right.

If everything works correctly, you should be able to set the cannon's elevation and see the value too:

In the next newsletter issue, Robert Cailliau continues this tutorial with a look at how to move objects around.

 
©2005 Runtime Revolution Ltd, 15-19 York Place, Edinburgh, Scotland, UK, EH1 3EB.
Questions? Email info@runrev.com for answers.