how can I give a variable a different number?

edited December 2016 in Questions about Code

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 ;)

Tagged:

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 doint 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 do i = 255;.

  • Answer ✓

    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!

  • Answer ✓

    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.

  • Answer ✓

    This is my approach:

    1. CTRL+T inside PDE
    2. CTRL+A to select whole code
    3. Hit TAB 2 times to pre-indent for the forum
    4. CTRL+C to copy it.
    5. Hit CTRL+Z 2 times to return code inside PDE to its original form before the TABs.
    6. Go to forum and hit CTRL+V to paste the pre-indented code.
    7. Finally click at Post Comment. :bz
  • edited December 2016

    @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:

    1. you declared a global variable i on line 1
    2. on line 8, you used the int 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.

Sign In or Register to comment.