We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Beginner scope question
Page Index Toggle Pages: 1
Beginner scope question (Read 220 times)
Beginner scope question
Mar 20th, 2009, 4:04pm
 
I've this code in a presentation:
Code:
int i;
void setup()
{
i = 50;
size(400, 400);
rectMode(CENTER);
}
void draw() {
background(192, 64, 0);
stroke(255);
fill(255);
rect(200, 200, 50, 50);
noStroke();
fill(#3E3AB4, i);
ellipse(mouseX, mouseY, 75, 75);
}
void mousePressed() {
i = i + 1;
}
The idea is that when you click the mouse, the circle gets darker, until it's opaque.  Clicking the mouse doesn't do anything though.  i've tried different variations on line 1, such as "public int i;", "public static int i;" and "static int i;".  The second to last line was originally "i++;" and that didn't work either.

Any suggestions?  I'm looking for an explanation of why this doesn't work, rather than a different implementation that achieves the same results.
Re: Beginner scope question
Reply #1 - Mar 20th, 2009, 4:13pm
 
In fact it does work, just not fast.

Two things you need to know:
1. opacity goes from 0-255, where 255 is 100% opacity.
2. a change of opacity between 50 and 51 is not visible. Even between 50 and 60 is difficult to see. To save you from RSI, I suggest to raise your steps from 1 to 10 or even bigger.


Soluation for your script:
i=i+1    -->   i=i+10 (or i+=10, which is the same)

Now it should work

p.s. You can also already give a value to int i when initializing it, so:
int i;   -->   int i = 50;   (and remove the line 'i=50;' from your setup void)
Re: Beginner scope question
Reply #2 - Mar 20th, 2009, 5:11pm
 
AHA! thanks that was driving me crazy.  sry for the noise
Re: Beginner scope question
Reply #3 - Mar 20th, 2009, 5:32pm
 
Also, you might want to change from mousePressed(), which only gets called when the user _releases_ the mouse button, to mouseDown(), which I believe fires continuously until the user lets go.
Page Index Toggle Pages: 1