Need help - Can't get my drawing to "move" using increments.

edited January 2016 in Questions about Code

As the title says, I've been stuck trying to get this piece of code to move. Any advice or help would be appreciated.

 void setup() {
  size(400, 400);
  smooth();
  background(113,181,202);
  }

void draw(){  
  background(113,181,202);
  int coordx = mouseX;
  int coordy = mouseY;

   if (mouseButton == LEFT){
    noStroke();
    fill(255, 118, 0);
    ellipse (coordx, coordy, 80, 35); 
    coordx = coordx+1;
    triangle (coordx - 60, coordy +18, coordx - 60, coordy - 18, coordx - 35, coordy);  
    coordx = coordx+1;
    fill(0,0,0);
    ellipse (coordx + 20, coordy -5, 10, 10);
    coordx = coordx+1;
    }  
  }

It needs to be drawn at the mouse cursor coordinate and move from there.

Tagged:

Answers

  • At the moment your drawing is set to be where the mouse is and is only displayed when the mouse is pressed. I made a version that initializes the drawing in the center of the screen, always displays it, and moves it slowly to the right:

    int coordx, coordy;
    
    void setup() {
      size(400, 400);
      smooth();
    
      // Initialize the drawing in the middle of the screen
      coordx = width/2;
      coordy = height/2;
    }
    
    void draw() {  
      background(113, 181, 202);
    
      // Display the drawing
      noStroke();
      fill(255, 118, 0);
      ellipse(coordx, coordy, 80, 35);
      triangle (coordx - 60, coordy +18, coordx - 60, coordy - 18, coordx - 35, coordy);  
      fill(0, 0, 0);
      ellipse(coordx + 20, coordy -5, 10, 10);
    
      // Move the drawing
      coordx = coordx+1;
    }
    
  • Cheers, but that wasn't what I need help with. I guess I should have specified that in the original post. I need help in regards to creating the drawing upon mouse click at the cursor and having it move to the left from where it was drawn.

  • You have the right idea, but you should only set coordx and coordy when you press the mouse. And they need to be defined outside of any methods, that way they keep their value between method calls.

Sign In or Register to comment.