For this exercise, we will create a button that gradually turns a lamp on when pressed, then off again when unpressed.
...
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 one that only emits a signal while it is pressed. When released, the signal turns off again.
...
Save the project and run the simulator. Pressing and releasing the button should change the Switch/Lamp image:
...
Changing a Value Gradually
Now, create another tag that will gradually change between 0.0 and 1.0:
...
Code Block |
---|
tag.write("isPressed", true); var amountPressed = tag.read("amountPressed"); do { amountPressed += 0.01; amountPressed = Math.min(1, amountPressed); tag.write("amountPressed", amountPressed); thread.msleep(10) } while (tag.read("isPressed")); |
To summarize how Here is what the script worksis doing, line by line:
Set the “isPressed” tag to true.
Get the current value and store it to the variable
amountPressed
.In a
while
loop, we do the following:Increase the value by a small amount (0.01).
Clamp the value so that it doesn’t go over 1.
Write the updated value to the opacity 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.
...