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 & HelpPrograms › Need some help
Page Index Toggle Pages: 1
Need some help (Read 1099 times)
Need some help
Mar 14th, 2010, 11:51pm
 
I've just started to use processing. Basically what I wanna do is to draw a circle, once drawn, the circle should then move straight down the
screen slowly until it is completely off the screen. (THIS PART I'VE DONE SUCCESSFULLY) lol

but then I want to click the mouse on a point, and my program should draw another circle of the
same appearance at that point and then move it downwards off the screen as before. I want this behavior to be repeated at the new mouse location each time a mouse button is clicked.
Quote:
int winwidth = 400;
int winheight = 300;
int x = 200;
int y = 150;


void setup () {

 size (winwidth, winheight);
 background (255);
 
}
void draw ()
{
 stroke (160);
 line (width/2, 0, width/2, height);
 line (0, height/2, width, height/2);
 fill (0);
 stroke (255);
 ellipse (200, y, 100, 100);
 y = y + 1;
}
void mousePressed (){

   
}


I'm having problems with mousePressed function... any ideas how this can be fixed ?
Re: Need some help
Reply #1 - Mar 15th, 2010, 1:05am
 
look here. here the balls bounce around instead of simply falling but the mouse interaction is exactly what you're looking for
Re: Need some help
Reply #2 - Mar 15th, 2010, 2:01am
 
here is some commented code.
http://processing.org/learning/topics/arraylistclass.html
there might be some new stuff for you like classes and arraylists, but thats actually the way to go.
Re: Need some help
Reply #3 - Mar 17th, 2010, 10:35pm
 
thanks guys ...
another question ...

I want my circle to appear in random colors and radius as soon as I press the mouse


this is my code so far
Quote:
int winwidth = 400;
int winheight = 300;
int x = 200;
int y = 150;
float r = random (255);
float g = random (255);
float b = random (255);
float radius = random (20,79);


void setup () {

 size (winwidth, winheight);
 background (255);
 
}
void draw ()
{
 stroke (160);
 line (width/2, 0, width/2, height);
 line (0, height/2, width, height/2);
 fill (0);
 stroke (255);
 ellipse (x, y, 100, 100);
 y = y + 1;

 }
 void mousePressed ()
{  
 background (255);  

y = mouseY + 1;
x = mouseX + 1;
float r = random (255);
float g = random (255);
float b = random (255);
float radius = random (20, 79);
fill (r, g, b);
ellipse (mouseX, mouseY, radius, radius);

   
}


there is definitely something wrong, I've spent 2 hours trying to figure out what I've done wrong ...

I see that the radius and colors are changing but they appear in the middle of the black circle...
Re: Need some help
Reply #4 - Mar 18th, 2010, 1:29am
 
A very common beginner's error: don't declare again your global variables (r, g, b, radius) in mousePressed, because you declare there local variables "masking" the global ones which remain unaffected.

Also, if you want to see balls and not trails, you should put a background() call in draw().

Last note: try and find better thread subjects. 99% of threads in Discourse ask for help, imagine if half of them were named "Need some help"... Smiley
Re: Need some help
Reply #5 - Mar 18th, 2010, 1:47am
 
so basically everything I've done in "mousePressed" I need to move to "Draw" ?
i've tried it and it gives me an error "unexpected token : void"

thanks again
Re: Need some help
Reply #6 - Mar 18th, 2010, 1:52am
 
loso25 wrote on Mar 18th, 2010, 1:47am:
so basically everything I've done in "mousePressed" I need to move to "Draw"

Where did I wrote that  Shocked
To be clearer, remove all the "float" declarations from mousePressed.

To be even more clearer, it is not enough because you don't use r, g, b in draw(). Here is a tested version:
Code:
int x = 200;
int y = 150;
float r = random (255);
float g = random (255);
float b = random (255);
float radius = random (20,79);


void setup () {

size (400, 300);

}
void draw ()
{
background (255);
stroke (160);
line (width/2, 0, width/2, height);
line (0, height/2, width, height/2);
fill (r, g, b);
stroke (255);
ellipse (x, y, 100, 100);
y = y + 1;
}
void mousePressed ()
{  
background (255);  

y = mouseY + 1;
x = mouseX + 1;
r = random (255);
g = random (255);
b = random (255);
radius = random (20, 79);
}

You had no trail because of the white stroke clearing the previous position of the circle (because you move at 1 pixel per frame). Yet the advice to use background() at start of draw() is generally useful (unless you want special effects).
Note also I have put constants in size(), as advised by the reference: this information is used when you export the sketch.
Re: Need some help
Reply #7 - Mar 18th, 2010, 2:13am
 
sorry I forgot to mention that firstly a black circle should fall down, and when I click the mouse I want circle to change its color and radius...

if it makes sense..
Re: Need some help
Reply #8 - Mar 18th, 2010, 5:20am
 
Just initialize r, g, b with 0 instead of a random number.
Page Index Toggle Pages: 1