mouseClicked / mousePressed

edited October 2014 in Questions about Code

This seems like a simple question, but I've looked all over and I can't figure it out.

I understand that, functionally mousePressed is to activate something while the mouse is held down and mouseClicked activates something when the mouse is clicked and than release (with the actual action being on the release). The problem I'm having is that I have a program with a mousePressed that I want to change to a mouseClicked, and the two commands work very differently.

I've tried everything and can't get it to work.

Here is the Bit I want to change...

angle=atan2((ytarget-y[0]),(xtarget-x[0]))+dsteer;
if (mousePressed==true) angle=PI+angle; //reverse direction if mouse pressed

And the whole program for reference (there are several generations of notes in here so they probobly don't make a ton of sense)...

color start=color(0,0,0);
color finish;
float amt = 0.0;

//Array of Lines


Streak[] Streak;
int nStreaks = 1000; //number of streaks

void setup() {
   noCursor();
  fill(start);
  finish=color(random(255),random(255),random(255));
   background(0);
  size(screen.width,screen.height);
  smooth();
  frameRate(60);
  //initialize monters with random parameters
  Streak = new Streak[nStreaks];
  for(int i=0; i<nStreaks; i++){
    Streak[i] = new Streak(random(1,1),random(10000,1),random(10000,10000),random(1,100000));

 //way streaks act ^^^^^^^^^^Speed?^^^^^^^^^^^^^^^^^^^^^^Straintess?^^^^^^^^^^^^^^^^^

  }
}

void draw() {
  for(int i=0; i<nStreaks; i++){
    Streak[i].update();
    Streak[i].draw();
  }
} 

class Streak {
  float xtarget; //x position of target
  float ytarget;
  float dtarget; //randomness for target
  float dmove; //randomness for movement
  float speed; //pixels per second 
  int diam=1 ; //diameter of line///////VARIABLE////////
  int nlinks=2; //number of links
  float[] x= new float[nlinks]; //x coordinates of streak
  float[] y= new float[nlinks];
  float dsteer; //steering offset in radians
  float angle;  //angle that streak is moving

  Streak(float _speed, float _dmove, float _dtarget, float _dsteer) {
    //randomize initial location 
    x[0]=random(0,width);
    y[0]=random(0,height); 
    for(int i=0; i<nlinks; i++){
      x[i]=x[0];
      y[i]=y[0];
     }
    speed=_speed;
    dmove=_dmove;
    dtarget=_dtarget;
    dsteer=_dsteer;
  }

  void update() {
    //find target, add some randomness
    xtarget=width/2;
    ytarget=height/2;   
    //move each body position up one
    for(int i=nlinks-1; i>0; i--){
      x[i]=x[i-1];
      y[i]=y[i-1];
    }

    //get angle to target
    angle=atan2((ytarget-y[0]),(xtarget-x[0]))+dsteer;
    if (mousePressed==true) angle=PI+angle; //reverse direction if mouse pressed

    //update position of head
    x[0]+=speed*cos(angle);
    y[0]+=speed*sin(angle);

    //keep within bounds
    if (x[0]>width) x[0]=width;
    if (x[0]<0) x[0]=0;
    if (y[0]>height) y[0]=height;
    if (y[0]<0) y[0]=0;     
  }

  //draw streak  
  void draw() {

   amt+=.00001;
  if (amt >=1) {
   amt = 0.0;
  start = finish;
 finish = color(random(255),random(255),random(255));
  } stroke(lerpColor(start,finish,amt));
    fill(lerpColor(start,finish,amt));
    for(int i=0; i<nlinks; i++){
      ellipse(x[i],y[i],diam,diam);
    }

  }
}

Answers

  • I understand what you mean. They probably act differently because mousePressed will do your command once the mouse is pressed. Doesn't matter how long or where unless specified it will happen. Mouse clicked only operates when the mouse is clicked at the standard mouse clicking rate. Click the mouse too fast, might not work. Click the mouse too slow, might give errors. I personally like using mousePressed better. Does that answer your question? Or did I miss the point. Please specify, thanks! :)

  • edited October 2014

    Thanks for getting back so fast!

    The problem isn't that clicking the mouse isn't working when the sketch is running, the problem is that I can't get the syntax right for the sketch to run at all.

    For example, it works fine like this...

    angle=atan2((ytarget-y[0]),(xtarget-x[0]))+dsteer;
    if (mousePressed==true) angle=PI+angle; //reverse direction if mouse pressed
    

    The first thing i tried was this...

    angle=atan2((ytarget-y[0]),(xtarget-x[0]))+dsteer;
    if (mouseClicked==true) angle=PI+angle; //reverse direction if mouse pressed
    

    I get "Cannot find anything named 'mouseClicked'".

    So I looked around and found this...

    int value = 0;

    void draw() {
      fill(value);
      rect(25, 25, 50, 50);
    }
    
    void mouseClicked() {
      if (value == 0) {
        value = 255;
      } else {
        value = 0;
      }
    }
    

    And tried to stuff the two together...

    angle=atan2((ytarget-y[0]),(xtarget-x[0]))+dsteer;
    void mouseClicked
    if (angle==angle) {angle=PI+angle;}
    if (angle==Pl+angle) {angle=angle;} //reverse direction if mouse pressed
    

    I get unexpected token "void".

    Also, I should tell you that I am using an old version (1.5).

    Any ideas? Thanks!

  • edited October 2014

    You won't get much help pasting unformatted code! [-(
    Please, highlight it and hit CTRL+K! >-)

  • OOHH...I was wondering how to make that work. Thanks.

  • Thank you for answering. Usually I get void unexpected when I forgot to close of the function before the void mousePressed. That could be a simple error. I would say, use the function that makes it work. If mousePressed makes some work, then put that. It's okay if you we both mousePressed and mouseClicked. Sometimes we just have to do it! I would recommend that you upgrade to a 2 processing. Your old sketches should work in there. It could help.

    :)

  • I thought at first that it might be a function that I forgot to close up, but than it wouldn't have worked before I made the change.

  • That is smart! But again, try to download more recent version.

  • There are 2 gr8 advantages the old v1.5.1 have that v2 & v3 lost: ;;)

    1. Export as unified ".jar" via CTRL+E.
    2. Better compatibility w/ JavaScript Mode (Processing.JS framework).
  • True. Hey MNGWA if you want, check out the Halloween Night of Code discussion. You can participate!

  • Yeah, In use the .jar export a lot. Also I'm on an old Mac OS so I can't update right now.

    I haven't been able to find anywhere that there is no mouseClicked command in 1.5, but I just can't figure out how to make it work.

  • https://processing.org/reference/mouseClicked_.html

    You should at least upgrade to v1.5.1 from your v1.5. (~~)

Sign In or Register to comment.