13.1.7 Exercise
For this exercise, we will create a button that gradually turns a lamp on when pressed, then off again when unpressed.
We will split this up into two parts:
Create a temporary push button (basic).
Use the button to gradually control a lamp object (advanced).
Making a Temporary Push Button
A temporary push button is a button that only emits a signal while pressed. When released, the signal turns off again.
First, make a tag called βisPressedβ to indicate whether the button is pressed or not. Do this by going to the Tag Editor pane, then choosing Add Tag:
In the Edit Tag window, use the settings shown below:
For the push button, weβll use a Switch/Lamp object. Press Insert β Switch/Lamp, then draw the object on the page:
Then, bind the βisPressedβ tag to the objectβs βValueβ property. This can be done by dragging the tag from the Tag Editor pane to the Basic Properties (Switch/Lamp) pane:
To make this look more like a push button, weβll choose a different image for each state. Use βPush Button Staticβ for state 0, and βPush Button Activeβ for state 1.
Now we will assign scripts to the buttonβs touch actions.
Select the button object. Then, in the βActionsβ pane, go to βOn Pressβ, then βActionβ, then choose βCreate New Scriptβ:
In the Script Editor, rename the script to βOn Pressβ, then replace the default script contents with the following:
tag.write("isPressed", true);
Next, assign a new script to the buttonβs βOn Releaseβ property in the same way. Name the script βOn Releaseβ. For this script, use the following contents:
tag.write("isPressed", false);
At this point, you should have the following in the Script Editor:
and you should have the following in the Actions pane for the button object:
When the button is pressed, the βOn Pressβ script will be triggered, setting the βisPressedβ tag to true.
When the button is released, the βOn Releaseβ script will be triggered, setting the βisPressedβ tag to false.
Save the project and run the simulator. Pressing and releasing the button should change the Switch/Lamp state:
Changing a Value Gradually
Now, create another tag that will gradually change between 0 and 100:
Instead of a single Switch/Lamp object, we will use two image objects to represent the lamp. We will bind a tag to the opacity of the βonβ image to simulate the lamp turning on and off gradually.
We will use the images β3D Lamp Off 010β and β3D Lamp On 010β from the Graphic Library:
Position the images so that they overlap, with the βonβ image displayed on top. Resize the images as desired. You should have the following:
Since the opacity property expects a value between 0 and 1, but the tag value will range from 0 to 100, we will use a scaling expression.
Select the βonβ image object, then click the binding icon next to its opacity property:
In the Binding Properties window, go to the Expressions tab. For the expression type, choose βScale Tag Valueβ, then use the following options:
Press OK to close the Binding Properties window.
If you run the simulator, you can manually change the value of the βamountPressedβ tag using the tag monitor. This should make the lamp look like it is off (0), on (100), or partially on (1β99).
Now we can modify the buttonβs scripts to gradually increase and decrease the value of the βamountPressedβ tag.
Go back to the βOn Pressβ script, then edit its contents so that you have the following:
tag.write("isPressed", true);
var amountPressed = tag.read("amountPressed");
do {
amountPressed += 1;
if (amountPressed > 100) amountPressed = 100;
tag.write("amountPressed", amountPressed);
thread.msleep(10);
} while (tag.read("isPressed"));
Here is what the script is doing, line by line:
Set the βisPressedβ tag to true.
Get the current value of the βamountPressedβ tag and store it to the variable
amountPressed
.In a
while
loop, wedo
the following:Increase the value by 1.
Clamp the value so that it doesnβt go over 100.
Write the updated value to the βamountPressedβ tag.
Sleep for a small amount of time (10 milliseconds).
When the βisPressedβ tag gets set to false, we end the loop. This happens when the user lets go of the button, in the βOn Releaseβ script.
The βOn Releaseβ script should be similar, with a few small changes:
tag.write("isPressed", false);
var amountPressed = tag.read("amountPressed");
do {
amountPressed -= 1;
if (amountPressed < 0) amountPressed = 0;
tag.write("amountPressed", amountPressed);
thread.msleep(10);
} while (!tag.read("isPressed") && amountPressed > 0);
Note that on line 8, we added the condition that amountPressed
must be greater than 0 for the cycle to continue. This means that once the value goes back down to 0, the script ends. This way, we donβt leave any script threads running in the background while the lamp is not in use.
Save the project and run it in the simulator. While the button is pressed, the lamp should gradually get brighter. When the button is released, the lamp should gradually get dimmer.
Β