Getting Started with Scripting


Excercise 9 - Controlling an object with a Joystick

Generally you create scripts by dragging tiles from the viewer; this gives you simple scripts (which already are syntactically correct, and are fully functional) to start out with, and then you proceed to modify them.

One essential operation is dropping a tile into another tile bearing the same type of information; this results in the old tile being replaced by whatever you dropped.

Thus, for example, Paint an airplane using the simple painting tools:

Drag from the PaintBrush, (or a crude green arrow from a Parts Bin) and drop somewhere on a Playfield. Paint a rough airplane-like figure facing upward, then hit the "Keep" button. This is your airplane. Open up its Viewer, and name it "airplane".

Make a script for the airplane that starts out:

airplane forward by 5
airplane turn by 5

Get a fresh Joystick from the Parts Bin, and bring up a Viewer for it. Go to the parts bank that shows the joystick-specific parts, "leftRight", "upDown", "angle", and "amount". Drag from the tile showing the word "leftRight"; tiles for "joystick's leftRight" will stick to the mouse.

DROP that "joystick's leftRight" directly onto the "5", to make the airplane's script read:

airplane turn by joystick's leftRight

Now make this script run all the time ("ticking"), and you can spin the airplane by moving the joystick's handle to the left or to the right.

A simple extension (left to the reader) is to add a "forward by" command to the same script, and have the amount by which it is to go forward be obtained from some other number that a Joystick is able to report. It's fun to explore various possibilities for mapping the joystick parameters into the parameters for the "turn" and "forward" commands. Since the Joystick delivers four different numbers, you could hook them up to other things about your airplane (such as its color or its scale factor) and end up with some rather unusual controls!