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 › Re: mousePressed won't work! New to processing
Page Index Toggle Pages: 1
Re: mousePressed won't work! New to processing (Read 1386 times)
Re: mousePressed won't work! New to processing
Nov 11th, 2009, 2:52pm
 
First: be nice and delete all those duplicate posts in the forum - looks like something somewhere got stuck Wink

Second have a look at Getting started; particularly the bit entitled 'Hello mouse'.

Hello mouse will explain that your code should include both setup() and draw().  Apart from size() most of your code would go in draw.  Although you could continue following that example and then write void mousePressed() at the end there's a simpler way to do what you want...

Look at the Reference, in particular mousePressed: that should be adequate for your current needs.
Re: mousePressed won't work! New to processing
Reply #1 - Nov 11th, 2009, 3:23pm
 
Yeah I checked that out and the furthest I could get is being able to run the program and not run into an error (but with the code for mouse pressed nothing would happen) so instead of getting an error message it would load but it would be as if my mousepressed code was never there (so there is no reactivity)
Re: mousePressed won't work! New to processing
Reply #2 - Nov 11th, 2009, 3:46pm
 
read the getting started part again, and read about using setup draw etc... mouse pressed only works in combination with draw
Re: mousePressed won't work! New to processing
Reply #3 - Nov 11th, 2009, 4:16pm
 
Yeah i used both like this and ended the draw } at the end of all my coding because when i put it at the end of the coding i pasted here it won't run the program saying there are other random errors (which were perfectly fine before and i know they aren't errors)

void setup() {
 size(600, 600);
 background(225, 225, 225);//This makes the background white//
}

void draw() {

if (mousePressed == true) {
 stroke(225);
 line(200, 375, 400, 375);
} else {
 fill(225);
 stroke(225);
 ellipse(300, 375, 70, 70);
}
Re: mousePressed won't work! New to processing
Reply #4 - Nov 11th, 2009, 4:25pm
 
Quote:
void setup(){
size(600,600);
smooth();
}

void draw(){
background(255, 255, 255);//This makes the background white//
monster();
mouth();
}

void monster(){
fill(0);
stroke(0);
ellipse(300, 300, 300, 300);//This is the black circle body of the monster//

fill(0);
stroke(0);
triangle(190, 200, 250, 200, 220, 90);//Monster ear left//

fill(0);
stroke(0);
triangle(350, 200, 410, 200, 380, 90);//Monster ear right//

fill(225);
stroke(225);
ellipse(300, 250, 50, 75);//This is the sclera of the monster eye//

fill(0);
stroke(0);
ellipse(300, 275, 20, 20);//This is the pupil of the monster eye//

fill(225);
stroke(225);
triangle(280, 300, 320, 300, 300, 320);//This is the nose of the monster//

fill(225);
stroke(225);
line(310, 310, 350, 310);//Right Whisker pointing straight//
fill(225);
stroke(225);
line(310, 310, 340, 340);//Right Whisker pointing down//
fill(225);
stroke(225);
line(310, 310, 360, 330);//Right Whisker in-between//
fill(225);
stroke(225);
line(290, 310, 250, 310);//Left Whisker pointing straight//
fill(225);
stroke(225);
line(290, 310, 260, 340);//Left Whisker pointing down//
fill(225);
stroke(225);
line(290, 310, 240, 330);//Left Whisker in-between//

fill(0);
stroke(0);
ellipse(300, 500, 100, 150);//Monster body//

fill(225);
stroke(225);
ellipse(330, 475, 20, 20);//Monster paw right//
fill(225);
stroke(225);
ellipse(270, 475, 20, 20);//Monster paw left//

fill(0);
stroke(0);
rect(330, 550, 60, 10);//Monster tail//

fill(0);
stroke(0);
ellipse(390, 550, 30, 30);//Monster tail end//
}

void mouth(){
if(mousePressed == true){
fill(225);
stroke(225);
ellipse(300, 375, 70, 1);//mouth closed//
  }else{
fill(225);
stroke(225);
ellipse(300, 375, 70, 70);//mouth opened//
  }
}

Re: mousePressed won't work! New to processing
Reply #5 - Nov 11th, 2009, 4:42pm
 
That's awesome! The coding all seems to make sense! Thank you soooo much!!!!!!! Grin Tongue Roll Eyes Smiley
Re: mousePressed won't work! New to processing
Reply #6 - Nov 11th, 2009, 5:06pm
 
Just wondering....would you happen to know how to make it work so that you can only close the mouth when you click on the mouth (so it is like eating the mouse pointer)?
Re: mousePressed won't work! New to processing
Reply #7 - Nov 11th, 2009, 5:49pm
 
Ha, dont tell me this isnt cool. He is really eating it Smiley


Code:
void setup(){
size(600,600);
smooth();
}

void draw(){
background(255, 255, 255);//This makes the background white//
monster();
mouth();
}

void monster(){
fill(0);
stroke(0);
ellipse(300, 300, 300, 300);//This is the black circle body of the monster//

fill(0);
stroke(0);
triangle(190, 200, 250, 200, 220, 90);//Monster ear left//

fill(0);
stroke(0);
triangle(350, 200, 410, 200, 380, 90);//Monster ear right//

fill(225);
stroke(225);
ellipse(300, 250, 50, 75);//This is the sclera of the monster eye//

fill(0);
stroke(0);
ellipse(300, 275, 20, 20);//This is the pupil of the monster eye//

fill(225);
stroke(225);
triangle(280, 300, 320, 300, 300, 320);//This is the nose of the monster//

fill(225);
stroke(225);
line(310, 310, 350, 310);//Right Whisker pointing straight//
fill(225);
stroke(225);
line(310, 310, 340, 340);//Right Whisker pointing down//
fill(225);
stroke(225);
line(310, 310, 360, 330);//Right Whisker in-between//
fill(225);
stroke(225);
line(290, 310, 250, 310);//Left Whisker pointing straight//
fill(225);
stroke(225);
line(290, 310, 260, 340);//Left Whisker pointing down//
fill(225);
stroke(225);
line(290, 310, 240, 330);//Left Whisker in-between//

fill(0);
stroke(0);
ellipse(300, 500, 100, 150);//Monster body//

fill(225);
stroke(225);
ellipse(330, 475, 20, 20);//Monster paw right//
fill(225);
stroke(225);
ellipse(270, 475, 20, 20);//Monster paw left//

fill(0);
stroke(0);
rect(330, 550, 60, 10);//Monster tail//

fill(0);
stroke(0);
ellipse(390, 550, 30, 30);//Monster tail end//
}

void mouth(){
if(mousePressed == true && dist(300,375,mouseX,mouseY)<30){
fill(225);
stroke(225);
noCursor();
ellipse(300, 375, 70, 1);//mouth closed//
}
else{
cursor(ARROW);
fill(225);
stroke(225);
ellipse(300, 375, 70, 70);//mouth opened//
}
}
Re: mousePressed won't work! New to processing
Reply #8 - Nov 11th, 2009, 5:50pm
 
You can also replace if(mousePressed == true) by one of these two in mouth()

Quote:
if(mousePressed && mouseX >= 265 && mouseX <= 335 && mouseY >= 340 && mouseY <= 410){


Quote:
if(mouseX >= 265 && mouseX <= 335 && mouseY >= 340 && mouseY <= 410){


This is a sort of detailed version of Cedric's example using dist().
Re: mousePressed won't work! New to processing
Reply #9 - Nov 11th, 2009, 6:01pm
 
WOOHOO! My cat monster can now eat the mouse! Thank you this was a huge help =D
Re: mousePressed won't work! New to processing
Reply #10 - Nov 11th, 2009, 6:50pm
 
i wouldt call it more detailed. Its more code but actually less accurate.  In this case dist makes sense as we want it to be inside a circle not a rectangular area.
Re: mousePressed won't work! New to processing
Reply #11 - Nov 11th, 2009, 8:31pm
 
Yes indeed, that's why i wrote "sort of" Smiley
Re: mousePressed won't work! New to processing
Reply #12 - Nov 12th, 2009, 2:45am
 
prister wrote on Nov 11th, 2009, 4:16pm:
Yeah i used both like this and ended the draw } at the end of all my coding because when i put it at the end of the coding i pasted here it won't run the program saying there are other random errors (which were perfectly fine before and i know they aren't errors)

void setup() {
 size(600, 600);
 background(225, 225, 225);//This makes the background white//
}

void draw() {

if (mousePressed == true) {
 stroke(225);
 line(200, 375, 400, 375);
} else {
 fill(225);
 stroke(225);
 ellipse(300, 375, 70, 70);
}


Works fine if you close draw(); change colours so what you draw is visible; and move background from setup() to draw().  That last bit is important since it ensures the sketch window is cleared and redrawn each frame; otherwise you draw on top of what went before which isn't always what you want...

Code:
void setup() {
size(600, 600);
}

void draw() {
 background(255);
 if (mousePressed == true) {
  stroke(0);
  line(200, 375, 400, 375);
 }
 else {
   fill(0);
   stroke(0);
   ellipse(300, 375, 70, 70);
 }
}


I have to say I don't particularly favour just providing people with code that solves their problem with little or no explanation (e.g. of why background should be in draw).  If the OP had followed both links I posted in my answer and done a bit of experimenting they should have been able to figure this out...  and that's usually a better way to learn.
Page Index Toggle Pages: 1