We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am not very skilled in Processing, but I have been trying to convert my sketch from mouse clicks to using Kinect hand states. How do I use variables from function1 in function2, when the variables were declared using function1's arguments?
void shapes(int posx, int posy)
{
pushMatrix();
translate(width/3,200);
for (int i=0; i<cols; i++) {
for (int j=0; j<rows; j++) {
int shapeIndex = shapeNumbers[i][j];
PShape currentShape = shapes[shapeIndex];
currentShape.setFill(colors[i][j]);
currentShape.setStroke(false);
shape(currentShape,i*posx,j*posy,shapesize,shapesize);
if (mousePressed ==true){
int x = i*posx+width/3;
int y = j*posy+200;
if (mouseX > x && mouseX < (x + shapesize) && mouseY > y && mouseY < (y + shapesize)){
colors[i][j]=color(255);
}
}
}
}
popMatrix();
}
void shapes(int posx, int posy){
pushMatrix();
translate(width/3,200);
for (int i=0; i<cols; i++) {
for (int j=0; j<rows; j++) {
int shapeIndex = shapeNumbers[i][j];
PShape currentShape = shapes[shapeIndex];
currentShape.setFill(colors[i][j]);
currentShape.setStroke(false);
shape(currentShape,i*posx,j*posy,shapesize,shapesize);
if (mousePressed ==true){
int x = i*posx+width/3;
int y = j*posy+200;
if (mouseX > x && mouseX < (x + shapesize) && mouseY > y && mouseY < (y + shapesize)){
colors[i][j]=color(255);
}
}
}
}
popMatrix();
}
Answers
Not sure why it is formatting wierd...
Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text
****EDIT: What functions are you referring to?
Kf
Thank you! I ended up finding out the answer
(fixed formatting, didn't fix terrible indentation. How do people live like that?)
@borgejor -- ust Ctrl-T in Processing IDE to auto-format code. This will make it easier to read.
Did you post the same function twice?
Several ways: instead of having local variables in function1 just use global variables. They must be unique and meaningful so instead of x maybe xInitialUserInput etc.
When you set these variables in a function they are changed globally and you can use them in the other function too
Example: function with a global variable
b
:Another way is that a function can return a variable like
Example for local variable in connection with a function that returns a value:
Remark:
you can also return an object (useful when you need to return multiple values at once) or an array or an array of objects from the function.
What doesn't work
What doesn't work is to change the variable (passed to the function as a parameter) in the function. It's not changing the value of the variable in setup (in the calling function. (since we have a
call by value
imho)see also
see also
https://github.com/Kango/Processing-snippets/wiki/programming-and-functions