Step 2Adding Tweak to your own sketches
Once you have added the Tweak.pde file to your sketch's folder, you can use Tweak variables for the things you want to be able to change while your program is running. If your sketch isn't finding the Tweak.pde file, just close and reopen your sketch (Processing looks for other .pde files in your sketch's folder when it is opened).
There are currently four basic Tweak variables: TweakFloat, TweakInt, TweakBool, and TweakPoint. These can each be used as standard Float, Int, Boolean, and Point types; just use the "new" operator to create them and get their value via the "val" property.
Perhaps an example will help at this point. Here we create a number (of type TweakInt) in the setup method and then get its value in the draw loop. The number's initial value is 100 and its description is "My changeable number". We then use that number to set the amount of red in the sketch's background.
----------------------------
TweakInt myNumber;
void setup(){
myNumber = new TweakInt("My changeable number", 100);
}
void draw(){
background(myNumber.val, 0, 0);
}
----------------------------
You can also set bounds on your Tweak variables. The example above works fine, but colors should normally stay between 0 and 255. In some cases, letting a variable go out of bounds may do unexpected things, so let's add in some limits to the example. The "1" in the TweakInt's instantiation specifies the step size (in other words, how quickly the number changes when you are adjusting the variable).
----------------------------
TweakInt myNumber;
void setup(){
myNumber = new TweakInt("My changeable number", 100, 1, 0, 255);
}
void draw(){
background(myNumber.val, 0, 0);
}
----------------------------Finally, let's add all three color channels to our example.
----------------------------
TweakInt myNumberR;
TweakInt myNumberG;
TweakInt myNumberB;
void setup(){
myNumberR = new TweakInt("Background Red", 100, 1, 0, 255);
myNumberG = new TweakInt("Background Green", 100, 1, 0, 255);
myNumberB = new TweakInt("Background Blue", 100, 1, 0, 255);
}
void draw(){
background(myNumberR.val, myNumberG.val, myNumberB.val);
}
----------------------------| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|










































