We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hi people! a couple of days ago I downloaded this amazing piece of software. i've been practicing a little with shapes, and I wanted to make a rectangle that starts with fill(255); and ends with fill(0); (up until then, no problem) and then, when the var reaches 0, set it back to 255. here's my code right now:
int i = 255;
void setup() {
size(900, 900);
background (255, 0, 0);
}
void draw() {
if (i == 0) {
int **i** = 255;
} else {
fill(i, i, i);
}
rect(300, 300, 300, 300);
i = i - 1;
}
in the bold i, processesing says the value of the local variable "i" is not used. I feel like this should work, but as I said, im new.
thanks in advance ;)
Answers
Please format your code by selecting it and click the 'C' button (or ctrl+o). I'll let it pass for now because it's quite simple and readable.
Processing is correct: that bold i is never used. It's because if you do
int i = 255;
, you actually create a new variable called i which is also an int! At that point you have two variables inside that if block with the same name and type but with a different value. To avoid this, just doi = 255;
.Format your code. Edit pst, select code and hit ctrol+o.
The problem is that you are defining a new variable i in your conditional. In other words, you have two places in your code where you create the variable >> i <<. Solution: Remove the second definition.
Kf
thank you both! it works now! (btw, I did format it, it just doesn't look as good here (although it's Ctrl + T for me))
ctrl+t works in the editor (IDE), ctrl+o is for the forums!
Ctrl+t works inside the Processing IDE. Ctrol+O is to format the code here in the forum.
Kf
And for the sake of those who are reading, you should first ctrl + t in the PDE, then paste in forum and ctrl + o to indent. You also must leave a line above and below.
This is my approach:
@GoToLoop -- that is so clear that it should be added to the forum Formatting sticky-post!
https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text
@P14082003 --
Your problem is that:
i
on line 1int
keyword to declare another local variable with the same name. You set the local variable to 255. The global one stays at 0. However, you never use that local variable, so you get a warning -- you probably aren't doing what you think you are!To fix the problem, get rid of the
int
keyword from line 8. Now you are changing the global variable as you wanted.Indeed. This answer would help those who don't know how to format code.