Functions and Input

Functions

Functions are portions of code within a program that does something specific. We've used some functions that are a part of the Processing language already. Functions have a name followed by parameters in parentesis. Like rect(100,100,50,50). The functions we've used so far work the same every time we use them, with the exception of how the parameters change them. In this unit we will be using functions that are specific to Processing, but you will program the behavior of.

Watch the video below and read Pages 51-52 in Getting Started with Processing.

What should you know?
  • Basic structure of Processing Sketch:
    1. Declare Variables
    2. void setup() - Runs once, typically includes functions that only need to run one time.
    3. void draw() - This function runs over and over again.
  • Variable Scope
    • Global variables are declared at the beginning of the sketch and can be used within any function
    • Local variables are declared inside of a function. Local variables only have meaning within a single function. So if a variable is declared within setup() you can not use it in draw().
  • How to use the print function
    • print() will print something in the console below the sketch
    • println () will print the parameter and then go on to the next line.
    • If the parameter is a variable the value of the variable will be printed
    • If text is put in quotes then the text will be printed. Text in quotes is referred to as a "String".

Mouse Input

In this unit we will add our first unser input. What I mean by this is you do stuff while the program is running and it responds to you! We're continuing through chapter 5 of Getting Started with Processing. The video below walks you through examples 5-4 thru 5-6. Watch the video, read 53-55, and play with the program examples. Changing things up a bit and see what happens.


Example 5-7 introduces the dist() function. Basically dist() simply finds the distance between any two points. Try the program below to see how it works. 

Try out Example 5-7. Play with it and see if you can make it cooler.



Another very useful function is map(). Map is used to rescale variables. You can think of it as dimensional analysis without all the mind-numbingness. The video below describes how map works.

Map has 5 parameters:

 map(number to convert, smallest possible number, biggest possible, new smallest, new biggest)
 
Reproduce my code for an "eye" and tweak it a bit to make it work better. You can even map things backwards if you want. Swap the positions of the new smallest and new biggest and see what happens. Also work through examples 5-10 and 5-11 from the book. 

Mouse Click

In this less we'll learn how to use the buttons on the mouse rather than just see where it is. Watch the video below and read pages 60-64 of Getting Started with Processing. This will get you started.

New Code
  • mousePressed - This is a boolean variable. It can be true or false. When true it stores the button pressed into the mouseButton.
  • mouseButton - When a button is pressed the state of mouseButton is changed to LEFT, RIGHT, or CENTER to reflect the button pressed.
  • if (statement) - This ask a question. if true the code in the following curly braces is executed.
  • else if (statement) - This can follow an if and will be used if the initial if is false. Otherwise it behaves as an if.
  • else - This can follow an if or else if. The code in the curly braces after the else will be executed when the preceding if  or else if is false.
Read through the pages 64-68 on determining location and try out all the code examples. 

Mouse Functions

Setup and Draw are not the only Functions built into Processing. The mouse variables can also be executed as functions. These functions will be run once when the event happens. This will interrupt whatever else is happening in the sketch. So, Draw loops over and over again, but if the mouse is clicked the code in void mousePressed() will execute immediately and then then the program will pick up exactly where it left off.

Try the code below:

Mouse Functions Available:
  • mousePressed() - Called when a mouse button is pressed. Stores the value of the button in the variable mouseButton
  • mouseReleased() - Called when a mouse button is released.
  • mouseClicked() - Called when a mouse button is pressed and then released
  • mouseMoved() - Called every time the mouse is moved and no buttons are clicked or held.
  • mouseDragged() - Called when the mouse is moved while a button is being held.

Comments